フィルターのクリア

Change to Text Colour to "RED" after a Logical statement is True and the "Return" Function NOT Terminating

2 ビュー (過去 30 日間)
Hi All,
Can you provide advice on the following Im trying to change the output text to "RED" if the second condition is met within my workspace?
Also I have several functions within the script calling several "Return" functions, how do I terminate the TOTAL script after the first return? It seems ALL second condition "Return" functions still run when the code should terminate after the first?
Many thanks.
if cableSpare > 0.2
fprintf('\nThe Ampacity for the Cable(s) or Conductors has the following Parameters:\n');
else
fprintf('\nRevise Cable Size to Meet Minimum Requirements for Ampacity;\n');
return
end

回答 (2 件)

Walter Roberson
Walter Roberson 2023 年 6 月 15 日
fprintf(2, '\nRevise Cable Size to Meet Minimum Requirements for Ampacity;\n'); %notice the 2
The color does not show up here, but does show up in the command window.
  1 件のコメント
Walter Roberson
Walter Roberson 2023 年 6 月 15 日
how do I terminate the TOTAL script after the first return?
Use error to terminate without the cooperation of the outer layers.
Note that code can typically use try, catch to notice that an error has occured and handle it smoothly.
It used to be the case that running out of memory was a condition that could not be caught, so to be sure of terminating code, one way used to be to deliberately request more memory than is possible to allocate. But these days it is possible to catch this error as well.

サインインしてコメントする。


DGM
DGM 2023 年 6 月 14 日
編集済み: DGM 2023 年 6 月 14 日
You can try using cprintf() from the File Exchange.
cableSpare = [0.1 0.3];
for k = 1:numel(cableSpare)
if cableSpare(k) > 0.2
fprintf('\nThe Ampacity for the Cable(s) or Conductors has the following Parameters:\n');
else
cprintf('red','\nRevise Cable Size to Meet Minimum Requirements for Ampacity;\n');
end
end
It's hard to say why your return is behaving as it is without seeing how the whole thing is written. It should return to the calling function, so if this is part of a function that's called elsewhere, then the calling function/script doesn't stop.

カテゴリ

Help Center および File ExchangeGraphics Objects についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by