How to use if statement when condition is a successful load of data? MatLab

1.8k Views Asked by At

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

3

There are 3 best solutions below

0
On BEST ANSWER

Check if the files exists before loading and processing:

if exist(filename,'file')
    ...
end
0
On

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
0
On

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