Problem with my Matlab code
Show older comments
Hello everybody; I'm using a open source script to make some calculation on my data (Raman spectra), I have a problem to read my input file. I have thousand files (.txt) each file contain 4 columns: X = Wavelength (cm-1) and Y = Raman intensity and Xpos, Ypos the coordinates of the position or the Raman spectra were collected. unfortunately the input file doesn't work ! Somebody could help me ? Thank you in advance
end
function Read_Raman_Files
% Reads Raman spectra from txt files.
% Each file contains the data for a single Raman spectrum:
% X = Wavelength (cm-1)
% Y = Raman intensity
% The name of the input file contains the coordinates at which the spectrum is taken.
% Results are stored in 'data.mat'.
end
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==1, X = data(:,i); end
Y(:,i) = data(:,2);
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
2 Comments
Star Strider
on 26 Feb 2017
You need to attach one of your text files so we can see what the problem is. (Use the ‘paperclip’ icon to attach it.)
Also, what are the two end calls doing ...
end % <— HERE ...
function Read_Raman_Files
% Reads Raman spectra from txt files.
% Each file contains the data for a single Raman spectrum:
% X = Wavelength (cm-1)
% Y = Raman intensity
% The name of the input file contains the coordinates at which the spectrum is taken.
% Results are stored in 'data.mat'.
end % <— ... AND HERE
Omar ben
on 26 Feb 2017
Answers (2)
Guillaume
on 26 Feb 2017
"I'm using a open source script to make some calculation on my data"
iThe script does no calcuation, It looks more like a script with a very narrow purpose: load all text files in the current directory and merge them into a single matrix, assuming that:
- the files only have two columns (or only the first two columns are useful)
- the first column is identical for all files (since it only uses the first column of the first file)
- the filename is of the form 'x__yXµm_n__Yµm_n__y.txt', where x can be anything, y can be anything and is optional, n is a number. If the filename does not match, a spectrum will be still be extracted but X and Y will be NaN.
- the files all have the same number of rows.
All in all, it's not particularly useful or well written (there's absolutely no error detection and no flexibility) and it's not suited to your files since a) they don't have the same format, b) they don't follow the same naming convention.
In any case, importing your data.txt file into matlab is trivial:
spectrum = readtable('Data.txt');
spectrum.Properties.VariableNames = {'Wavelength', 'Intensity', 'Xpos', 'Ypos'};
with the added benefit that it loads it in a modern (table) useful format.
1 Comment
Image Analyst
on 26 Feb 2017
Edited: Image Analyst
on 26 Feb 2017
0 votes
That data in data2.txt does not look like what you described, which is x, y (signal), xPosition, yPosition. By the way, it's not "wavelength", it's "wavenumber", leading me to think the author didn't know much about Raman. With my Raman spectra the whole spectra is taken at one x,y location. Indeed, moving the substrate while a measurement is being taken will give bad data. There may be additional curves in my files that are spectra taken at different depths into the surface being studied. But you won't have a new location for every single wavenumber. Please explain that. It just doesn't make sense to me.
I think what you actually have is one of those European number formats where the decimal point is a comma instead of a dot/period. In that case, it makes sense and you have two columns of numbers: the wavenumber, and the Raman signal separated by a space. I don't use that kind of number format so I don't know how to tell it to read in numbers with comma instead of dot, but I bet Guillaume knows how.
Categories
Find more on Get Started with MATLAB 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!