Extract data from text file and save them to 3D array
9 次查看(过去 30 天)
显示 更早的评论
Hello! I have a small question here. I got some text files in form as following:
08-September-2015 10:44
28 lines in following concentration table = NCONC+1
Conc. %SD /Cr+PCr Metabolite
0.599 2% 0.106 Mac
2.216 6% 0.393 Ala
0.000 999% 0.000 Asp
......
There are some hundred lines following, too. However, I only need the first element from the fifth line (2.216), how can I extract it?
Besides, I have hundreds of this kind of files, from which I need to extract this element from above mentioned place. The files are created from a for loop with three variables. I want to save the extracted elements into a 3D array according to these three variables. Can someone help me on this?
I wrote some codes to open the files, the variables of the for loop are in the codes:
directory= 'C:\Users\karl\Documents\COORD\';
for lb=19:11:30;
for snr=20:105:230;
for GC=2.5:0.025:2.55;
filename = [directory, 'Gln', num2str(lb), 'HzSNR', num2str(snr), 'C' , num2str(GC), 'Mice'];
fid=fopen(filename);
fclose(fid);
end;
end;
end;
So, the three variables are lb, snr and GC, the files which contains the data I need are also named in a way after these three variables.
Can someone help me? Thank you very much!
0 个评论
采纳的回答
Walter Roberson
2015-9-9
lbvals = 19:11:30;
num_lb = length(lbvals);
snrvals = 20:105:230;
num_snr = length(snrvals);
GCvals = 2.5:0.025:2.55;
num_GC = length(GCvals)l
Your3DMatrx = zeros(num_lb, num_snr, num_GC);
for lbidx = 1 : num_lb
lb = lbvals(lbidx);
lbstr = num2str(lb);
for snridx = 1 : num_snr
snr = snrvals(snridx);
snrstr = num2str(snr);
for GCidx = 1 : num_GC
GC = GCvals(GCidx);
GCstr = num2str(GC);
filename = fullfile(directory, sprintf('Gln%sHzSNR%sC%sMice', lbstr, snrstr, GCstr));
fid = fopen(filename, 'rt');
datacell = textscan(fid, '%f', 1, 'HeaderLines', 4);
fclose(fid);
Your3DMatrx(lbidx, snridx, GCidx) = datacell{1};
end
end
end
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
