Using throw error to validate string input

1 回表示 (過去 30 日間)
Damien Williams
Damien Williams 2019 年 5 月 7 日
コメント済み: Damien Williams 2019 年 5 月 7 日
Im trying to validate the input string in my function using error(msg), can anyone help?
function [amino_acid_chain] = synthesize(mRNA)
% throws an error if input string 'mRNA' is not a member of y.
y = ['g','c','a','u','G','C','A'',U'];
x = ismember(mRNA,y);
% I want to use this error if anything comes up logical 0 in x
msg = 'Error occurred.';
error(msg)
  3 件のコメント
Rik
Rik 2019 年 5 月 7 日
Also note that it is probably easier for the rest of your script to pick either upper or lower case. You can use the upper or lower function to convert your input, then you can continue your function with only one case to worry about.
Damien Williams
Damien Williams 2019 年 5 月 7 日
I have a habit of over complicating things! thanks.
Ive already converted input to a cell array in groups of three and uppercase.

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

採用された回答

Rik
Rik 2019 年 5 月 7 日
if any(~x)
error(msg)
end
  1 件のコメント
Damien Williams
Damien Williams 2019 年 5 月 7 日
I wasnt using "any", thankyou for this.

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

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by