loading .txt file

6 ビュー (過去 30 日間)
Jonathan
Jonathan 2011 年 10 月 26 日
Hi, How do I load a file like this (3 lines):
-------------
#A=AY/func/7_corrs/broca_4mm_Z_2standard.nii.gz B=AY2/func/7_corrs/broca_4mm_Z_2standard.nii.gz
--------------
#A #B #(A uni B) #(A int B) #(A \ B) #(B \ A) %(A \ B) %(B \ A) Rx(B/A) Ry(B/A) Rz(B/A)
--------------
127239 173524 204106 96657 30582 76867 24.0351 44.2976 0.9209 0.9803 1.3314
---------------
What I want to extract in Matlab is..
#A = 127239
#B = 173524
#(A uni B) = 204106
etc...
Thank you,
  2 件のコメント
Matt Tearle
Matt Tearle 2011 年 10 月 26 日
Can you please fix the formatting to show how the file appears. Thanks. Also, can you explain more how you want MATLAB to extract and package the information: do you want strings "#A = 127239" or variables A, B, etc with values 127239, ... Do you want those labels (A, B, ...) to be fixed or taken from a line of the file. And so on.
Fangjun Jiang
Fangjun Jiang 2011 年 10 月 26 日
use {}Code format next time.

サインインしてコメントする。

採用された回答

Matt Tearle
Matt Tearle 2011 年 10 月 26 日
If you don't need individual variable names then I'd probably go with the simplest approach of
data = dlmread('data.txt','',2,0);
If you have missing values or error codes or other weirdness, then use textscan.
If you have Statistics Toolbox, another approach would be to use dataset to import the data as a dataset array:
data = dataset('File','data.txt','headerlines',1);
The downside is that the variable names will be hideous, due to all the non-valid characters. If you wanted to get vaguely fancy (and had some pattern to how to interpret the names), you could use regexp:
fid = fopen('data.txt','rt');
hdr = fgetl(fid);
hdr = regexp(fgetl(fid),'\t','split');
fclose(fid);
hdr = regexprep(hdr,'#','No_');
hdr = regexprep(hdr,'%','Perc_');
hdr = regexprep(hdr,'\W','');
data = dataset('File','data.txt','headerlines',1);
data.Properties.VarNames = hdr

その他の回答 (1 件)

Jonathan
Jonathan 2011 年 10 月 26 日
  • #A=AY/func/7_corrs/broca_4mm_Z_2standard.nii.gz
  • #A #B #(A uni B) #(A int B)
  • 127239 173524 204106 96657
Hi The bullets indicate the 3 lines.I would like the numbers with any label. i.e. I want to be able to call B=173524 OR line3nos(2)=173524
Thank you, S

カテゴリ

Help Center および File ExchangeGraph and Network Algorithms についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by