Spectral processing: Batch reading of files with co-ordinate data
1 view (last 30 days)
Show older comments
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 Comments
Alvery
on 15 Dec 2020
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
Answers (0)
See Also
Categories
Find more on Data Import and Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!