Spectral processing: Batch reading of files with co-ordinate data

1 次查看(过去 30 天)
I have data files with the name "X_Y.txt" where X and Y referes to X(µm),Y(µm) position of spectral files. I have such ~280 files fetched from a rectangular grid, each column in the grid have one such file coded with its X,Y position in the name.
Additional info: Each *.txt file, is 2 columns 2000 rows, contains spectral intensity corresponds to the frequency
Aim: To generate spectral maps (2D map(X-Y),)of specific wavenumber (with ranging intensity)
I have a program, but encounter some issues in the first level(ire, while reading data).
files = dir('*.txt');
Ncurves = length(files);
if Ncurves==0, display('No txt files found!'); return; end
for i = 1:Ncurves,
i
fname = files(i).name;
data = importdata(fname);
if i==0, X = data(:,i); end
Y(:,i) = data(:,1);
dash = strfind(fname,'__');
Xpos(i) = str2num(fname(strfind(fname,'Xµm_')+4:dash(2)-1));
Ypos(i) = str2num(fname(strfind(fname,'Yµm_')+4:dash(3)-1));
end;
save('data.mat', 'Ncurves', 'X', 'Y', 'Xpos', 'Ypos');
return
The error comes when I run
for i = 1:Ncurves,
i
fname = files(i).name;
data = importdata(fname);
if i==1, X = data(:,i); end
Y(:,i) = data(:,1);
dash = strfind(fname,'__');
Xpos(i) = str2num(fname(strfind(fname,'Xµm_')+4:dash(2)-1));
Ypos(i) = str2num(fname(strfind(fname,'Yµm_')+4:dash(3)-1));
end;
While running the entire code I got error messages as
i =
1
Index exceeds array bounds
can someone help me to sort out this issue..
  3 个评论
Manas M
Manas M 2020-12-13
编辑:Manas M 2020-12-13
Thanks Alvery for looking in to issue with my project
I am a newbe in matlab
The first file is 0_10.txt, I have 7 such files up to 0_17.txt and rest 1_11.text to 16_17.txt.
i tried to run each line the code fails at "Y(:,i) = data(:,1)" and the error is " Unable to perform assignment because the indices on the left side are not compatible with the size of the right side"
Alvery
Alvery 2020-12-15
I think I can guess the problem. When you make the assignment into Y, the size(Y) will have a value. If size(Y) = [0,0] i.e. it's empty, at the beginning, then no problem it just makes Y big enough to fit the data. If you have already made an assignment to Y, and size(Y) = [8,1] for example, then you load data, and size(data) = [7,2] i.e. it has a different number of rows, then Matlab can't make the assignment Y(:,i) = data(:,1) becasue it doesn't fit.
If you need to capture data with different numbers of rows, you need to use a cell-array. Rather than try to explain cell-arrays here, probably best if you look at the Matlab Doc. https://uk.mathworks.com/help/matlab/cell-arrays.html
Indexing them is a bit more complex. If you have problems with the indexing, a tactic that really helps is to put in a breakpoint just before the problematic line, then use the "evaluate" function or command-line to try out variations of the expression until you find out what works. This isn't a trick you can do in a compiled programming language, and is one of the joys of using an interpreter.
Cheers,
Alvery

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Import and Analysis 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by