For Loop stops using either textread or textscan to read in text files
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1156668/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1156673/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1156678/image.png)
Hi,
%The CALSB.LIS contains all 300 spectrum file = spectrum name, concentration (e.g. calsb.341, 7)
%Each spectrum, calsb.# (e.g. calsb.341) contains (wavelenght, intensity) as shown in the image. I have 300 of these files, each having 519 wavalenghts and 519 intensities.
% I try to store all the intensities for each spectrum in xc (300 X 519).
% Using the loop below I was able to store all the intensities but the loop stops after the 75th iteration (only reading 75 spectra out of 300)
%% Reading in calibration data
[fname1, cc]=textread('CALSB.LIS','%s %f');
fname1=erase(fname1, ',');
m=length(cc);
%fname1= calibration file name
%cc= calibration concentration
for i=1:m
[wn1,inten]=textread(fname1{i},'%s %s');
%the wavelength for each calibration doesn't change
%New values of the intensity is generated for each spectrum
if i==1
n=length(wn1);
xc=zeros(m,n);
%storing the first Calsb file (spectrum) reading the wavelength and intensity
%wavelength for each calibration doesn't change but intensity changes
wn1=str2double(wn1);
inten=str2double(inten);
xc(i,:)=inten';
wnc=wn1';
else
%storing the intensity for other Calsb file (spectrum) are stored
inten=str2double(inten);
xc(i,:)=inten';
end
end
%error
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1156683/image.png)
I tried using textscan instead of textread and also the loop stops after the 75th iteration (only reading in 75 spectra out of 300)
% Reading in calibration data
[fname1, cc]=textread('CALSB.LIS','%s %f');
fname1=erase(fname1, ',');
m=length(cc);
fname1= calibration file name
cc= calibration concentration
for i=1:m
fid=fopen(fname1{i}, "r");
[all]=textscan(fid,'%f %f');
wn2= all{1};
inten2 =all{2};
fclose(fid);
wnc=wn2';
%storing the first Calsb file (spectrum) reading the wavelength and intensity
%wavelength for each calibration doesn't change but intensity changes
if i==1
n=length(wn2);
xc=zeros(m,n);
%storing the wavelength and intensity
wn2=str2double(wn2);
inten2=str2double(inten2);
xc(i,:)=inten2';
wnc=wn2';
else
%storing the intensity for other Calsb file (spectrum) are stored
inten2=str2double(inten2);
xc(i,:)=inten2';
end
end
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1156688/image.png)
Please, what can I do differently? Thanks for your help.
2 comentarios
Geoff Hayes
el 14 de Oct. de 2022
@Qudus Thanni - perhaps the 75th file doesn't exist? You may want to check this with isfile to see if the file exists. If not, you could log an error and then continue with the next file. Something like
for i=1:m
if ~isfile(fname1{i})
fprintf('Cannot find file %d with name %s\n', i, fname1{i});
continue;
end
[wn1,inten]=textread(fname1{i},'%s %s');
% etc.
Respuestas (0)
Ver también
Categorías
Más información sobre Data Import and Export en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!