필터 지우기
필터 지우기

For Loop stops using either textread or textscan to read in text files

조회 수: 1 (최근 30일)
Qudus Thanni
Qudus Thanni 2022년 10월 14일
댓글: Qudus Thanni 2022년 10월 14일
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
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
Please, what can I do differently? Thanks for your help.
  댓글 수: 2
Geoff Hayes
Geoff Hayes 2022년 10월 14일
@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.
Qudus Thanni
Qudus Thanni 2022년 10월 14일
@Geoff Hayes Thank you, I was able to fix it. That part of the data was missing.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Import and Export에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by