Clear Filters
Clear Filters

Can I assign NaN to output of iterative loops that are caught, and print which inputs are caught?

1 view (last 30 days)
I have a code that runs a series of iterative loops. I have the following code:
if true
for x=1:10
for i=1:z
try
B=50*i;
[BTRAIN,CNTER]=replica(TNSPEC,B);
[sds,sdskew,qrr] = qb(TNSPEC,BTRAIN,newspec,CNTER,radfrac,sensitiv);
bias=sds-expected_standard_deviation(j);
SDS_matrix((i),:)=[compass_points(j,:),B,sds,bias,sdskew];
catch
end
end
end
end
The last line, the SDS_matrix, gets averaged over six runs, and then assigned to a cell array. That 'catch' has been really important because it has caught a lot of inputs that generate errors in the function being called. My problem is, though, that I think that sds, sdskew, and qrr are staying assigned the last iteration's values before the next functional iteration occurs. Is there a good way to mark the inputs that raised errors, maybe fill in NaN values, so I can pull those rows out of my final matrix? Also, is there a way to generate an error log that tells me which error was raised by the inputs that got caught?
Thanks.
*Edit - to clarify, when I look at my final matrix, there are no holes in the data input to show that a value assignment was skipped, though I know some iterations have been caught. *

Accepted Answer

Walter Roberson
Walter Roberson on 27 Jun 2018
for x=1:10
for i=1:z
B=50*i;
sds = nan; bias = nan; sdskew = nan;
try
[BTRAIN,CNTER]=replica(TNSPEC,B);
[sds,sdskew,qrr] = qb(TNSPEC,BTRAIN,newspec,CNTER,radfrac,sensitiv);
bias = sds-expected_standard_deviation(j);
catch ME
end
SDS_matrix(i,:)=[compass_points(j,:),B,sds,bias,sdskew];
end
end
This will get as many of the values as could be computed with nan for the rest.
However, you are not using "x" inside your for loop, and you are always writing to the same row of the SDS_matrix. Also you are using j inside your loop but never change it, leaving us wondering if your j and x should be the same variable. If not, if j is set before the loop, then it is not clear why you would want to copy compass_points(j,:) as the first entry to all of the rows...
  1 Comment
Cynthia Dickerson
Cynthia Dickerson on 27 Jun 2018
Thanks! The outer loop is actually more complicated; it determines some of the input variables for the called function. The whole code is a couple hundred lines. The snippet is actually repeated 5 times (a holdover from an old edit) rather than being assigned based off of the iteration count.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!