skipping files in for loop

7 次查看(过去 30 天)
Hello, I have multiple multiple files with similar names like 20210615_sh05_Ref.spe and 20210615_sh05_.spe.
I want to process only the files that contain the word Ref in it but my code is still processing all of them. I am assuming I am not indicating well the indices that I want to extract from strfind(w) but im not sure how to fix it.
Essentially, if the file contains the word reference, then process it, if not, then skip to the next file. Then I want to gather those results in an aray so I can average them.
Where did I go wrong? Thank you a lot!! (sorry for the language mix on my script I ran out of variable names)
tipodearchivo= dir('20210615_sh*.spe'); %Find all spe files in folder
nombres = {tipodearchivo.name};
cantidad = length(nombres);
cuenta = 0 ;
% Find reference files and take em out of the loop
w= strfind(nombres,'Ref');
for s=1:cantidad %Itiretae over shot files
nombres{s};
theresults=zeros(9,2);
Resultados=zeros([],5);
if ~isempty(w)%If h is not empty, then its a reference image, calculate
for t=1500:-1:600 %t=150:50:1600 %Time;
for g=150:100:1608
for u=g+100
[z,pos]=min (s1(g:u,t));
minpixel=g+pos;
[z1,pos1]=min (s1(u:u+100,t));
minpixel1= u+pos1;
Size= minpixel1-minpixel;
cuenta = cuenta+1 ;
Resultados(cuenta,:)=[cuenta t g u Size];
end
end
end
AverageSizeOneShot=mean(Resultados(:,5))% Size of one fringe Average of 101Pixels seems fine for shot 5
end
tamanios=mean(AverageSizeOneShot);
theresults(9,:)=[tamanios];
end
  1 个评论
Jan
Jan 2021-7-22
Some hints:
nombres{s}; % Omit this, because this does nothing
for g=150:100:1608
for u=g+100 % This is not a loop. Use this for clarity:
u = g + 100;
theresults(9,:) = tamanios; % No need for square brackets

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2021-7-22
编辑:Walter Roberson 2021-7-22
Change
if ~isempty(w)%If h is not empty, then its a reference image, calculate
to
if ~isempty(w{s})%If h is not empty, then its a reference image, calculate
However... you could instead
tipodearchivo= dir('20210615_sh*Ref.spe'); %Find all Ref spe files in folder
and then you would not need any filtering
Also, instead of the approach you are using with strfind() and isempty, you could instead use
w = contains(nombres,'Ref');
stuff
if w(i)

更多回答(0 个)

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by