How to display error using error function?

3 ビュー (過去 30 日間)
Porgs
Porgs 2018 年 5 月 1 日
コメント済み: Walter Roberson 2019 年 8 月 12 日
I have a program that computes thermal conductivity based on an input of a certain alloy and temperature. I would like to have the function display an error if the wrong alloy is inputted. Would It be possible to do this with the error function? This is the code I have so far.
function k = ThCond(alloy,T) % T = input temperature
k = zeros(1,numel(T));
for a = 1:numel(T)
if (298 <= T(a)) && (T(a) <= 840) && strcmp(alloy,'Al2')
k(a) = 149.7 + 0.0809.*T(a) - (1.*10^(-4)).*(T(a).^2);
elseif (298 <= T(a)) && (T(a) <= 773)
k(a) = 76.64 + 0.2633.*T(a) - (2.*10^(-4)).*(T(a)^2);
end
if (293 <= T(a)) && (T(a) <= 890) && strcmp(alloy,'Al3')
k(a) = 124.7 + 0.56.*T(a) + (1*10^(-5)).*(T(a)^2);
end
if (100 <= T(a)) && (T(a) <= 1200) && strcmp(alloy,'Cu1')
k(a) = 453.9 - 0.1054.*T(a);
end
if (460 <= T(a)) && (T(a) <= 1188) && strcmp(alloy,'Cu2')
k(a) = 140.62 + (112.14.*10^(-4)).*T(a);
end
if T(a) <= 1443 && strcmp(alloy,'Cu3')
k(a) = 16.041 + (438.9.*10^(-4)).*T(a);
end
if (400 <= T(a)) && (T(a) <= 1000) && strcmp(alloy,'St1')
k(a) = 76.63 - 0.0459.*T(a);
end
if (298 < T(a)) && (T(a) < 1573) && strcmp(alloy,'St2')
k(a) = 6.31 + ((27.2.*10^(-3)).*T(a)) - (7.*10^(-6)).*(T(a).^2);
end
if T(a) <= 1727 && strcmp(alloy,'St3')
k(a) = 20 + (61.5.*10^(-4)).*T(a);
end
end
end
  3 件のコメント
Porgs
Porgs 2018 年 5 月 1 日
Not really, I would like to use the error fuction to display a personal error message that I write, to the user.
Walter Roberson
Walter Roberson 2018 年 5 月 1 日
error('Alloy must be H2SO4 or CuTi2C6H8') ;

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

採用された回答

Wick
Wick 2018 年 5 月 1 日
Simple enough then.
err = 'This is an error message.'; % perhaps something a bit more descriptive
error(err);

その他の回答 (3 件)

Porgs
Porgs 2018 年 5 月 1 日
Where would I put that in my code because I'm getting an error message when I'm trying to use this.
  1 件のコメント
Wick
Wick 2018 年 5 月 1 日
That's literally what the 'error' function does. It displays an error and breaks execution (unless it's part of a try,catch structure). So you'd put it as the result of an 'else' statement or possibly as result of an 'otherwise' option in a switch,case structure.
If you you don't want an actual error message and just want text displayed to the screen you can use the 'disp' or 'warning' commands instead.

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


Porgs
Porgs 2018 年 5 月 1 日
Ok thanks

usha shree
usha shree 2019 年 8 月 9 日
one dimesional error with two elements
  1 件のコメント
Walter Roberson
Walter Roberson 2019 年 8 月 12 日
Could you expand upon that remark?

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

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by