0

So In a loop, I want all statements to be executed only if the load if data in that loop is successful. Else I want the loop to continue to the next iteration.

       for l=1:.5:numfilesdata

     if H(x,y)= load( ['C:\Users\Abid\Documents\MATLAB\Data\NumberedQwQoRuns\Run' num2str(t) '\Zdata' num2str(l) '.txt']);


      %%%%%Converting Files
      for x=1:50;
          for y=1:50;
           if H(x,y)<=Lim;
              H(x,y)=0;
           else
              H(x,y)=1;
          end
          end

          A(t,l)=(sum(sum(H))); %Area

          R(t,l)=(4*A(t,l)/pi)^.5; %Radius
      end
      end

As you can see I am incrementing by .5, and if the load doesn't work on that increment I want the loop to essentially skip all the operations and go to the next step.

Thank You, Abid

Shai
  • 111,146
  • 38
  • 238
  • 371
Abid
  • 270
  • 8
  • 18

3 Answers3

1

Check if the files exists before loading and processing:

if exist(filename,'file')
    ...
end
cyborg
  • 9,989
  • 4
  • 38
  • 56
1

I'm not quite certain of this line:

if H(x,y)= load( [...]); %# This tries to load dat file at x,y position in `H`

x and y seem unknown at the first loop iteration, then fallback to 50,50 (last index of subsequent loop).

You may try:

H = load( [...]); %# This tries to load dat file in `H`

if numel(H) ~= 0
  %# iterate over H
end
Laurent'
  • 2,611
  • 1
  • 14
  • 22
1

You could use a TRY/CATCH block:

for i=1:10
    try
        H = load(sprintf('file%d.txt',i), '-ascii');

        %# process data here ...

    catch ME
        fprintf('%s: %s\n', ME.identifier, ME.message)
        continue

    end
end
Amro
  • 123,847
  • 25
  • 243
  • 454