Matlab警告,用向量替换字符

时间:2014-05-02 14:41:00

标签: matlab warnings

我有一个包含循环的算法。在循环中,完成了几个计算,并针对某些边界情况发出警告。我没有每次都显示警告,而是希望在包含循环的所有索引的循环之后发出警告。

所以而不是

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

我想

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

现在我试过

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

但那只是打印

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

而不是

Warning: Something went wrong for j = 1 45.

3 个答案:

答案 0 :(得分:4)

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

例如:

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

返回

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

答案 1 :(得分:3)

您可以尝试:

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

例如:

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

会回来:

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

答案 2 :(得分:1)

将错误定义为

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))])

这是一种可能的解决方案。