Question

I have an algorithm containing a loop. In the loop several calculations are done and a warning is issued for some border cases. Rather than having the warning show up each time, i would like to have a warning after the loop containing all the indices of the loop where something went wrong.

So instead of

for j=1:T
    warning('Something went wrong for j = %d',j)
end

I would like to have

for j=1:T
   ...
end
warning('Something went wrong for j = %d',IndicesOfJ)

Now I tried

warning('Something went wrong for j = %d',[1,2])

But that just prints

Warning: Something went wrong for j = 1.Something went wrong for j = 45.

instead of

Warning: Something went wrong for j = 1 45.
Was it helpful?

Solution

errflag = 0
% your loop. If warning criteria are met, set errflag to 1 and store the index.
if errflag
    warning('Something went wrong for j = %s',sprintf('%u ',IndicesOfJ));
end

For example:

IndicesOfJ = [1 3 4 5];
warning('Something went wrong for j = %s',sprintf('%u ',IndicesOfJ));

Returns

Warning: Something went wrong for j = 1 3 4 5  

OTHER TIPS

You can try :

warning('Something went wrong for j = %s',mat2str(IndicesOfJ))

For example :

>> warning('Something went wrong for j = %s',mat2str([1,2]))

Would return :

Warning: Something went wrong for j = [1 2] 

Define error as

error = zeros(1,T);

for i = 1:T

  if condition is met

    error(i) = 1; 

  end

end

warning(['Something went wrong for j = ' num2str(find(error))])

This is a possible solution.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top