Checking the variable for specific string

Very new to Matlab. I need to give 'high' or 'low' in one function's input as shown below. How can I make sure that the variable only accept 'high' or 'low'? If anything else, then there will be an error like 'filter type error'.
Using code bellow, gives error even with 'high' and 'low' input.
Thanks!
function [Y] = butter2filtfilt(x, Fs, Fcutoff, Type)
if Type~= "high" || Type~= "low"
error ('Filter type error')
end
end
>> LF = butter2filtfilt(ecg, 257, 0.15, 'low');
Error using butter2filtfilt (line 6)
Filter type error

1 件のコメント

Steven Lord
Steven Lord 2019 年 7 月 31 日
You've received a couple alternative solutions, but as an explanation for why this doesn't work that if statement is equivalent to "If type is not equal to 'high' or type is not equal to 'low', throw an error".
So what if type is equal to 'high'? The "type is not equal to 'high'" portion of that sentence is false, but the "type is not equal to 'low'" portion is true. Since the condition is false or true, the if is satisfied and so you throw an error.
The same holds if type is equal to 'low', just in reverse order. The first portion is true and the second portion is false.
You want to error if type is not equal to 'high' and type is not equal to 'low', not if one or the other is true.

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

 採用された回答

Alex Mcaulley
Alex Mcaulley 2019 年 7 月 31 日

3 投票

function [Y] = butter2filtfilt(x, Fs, Fcutoff, Type)
if ~ismember(Type,{'high','low'})
error ('Filter type error')
end
end

5 件のコメント

Adam Danz
Adam Danz 2019 年 7 月 31 日
編集済み: Adam Danz 2019 年 7 月 31 日
The best solution should allow for case insensitivity. If the user enteres "High" for example, it will cause an error. One way to allow for case insensitivity is demonstrated in my answer. If you're going to use ismember() intsead, and if they options are all lower case, then you should implement this instead of what's currently in the accepted answer.
~ismember(lower(Type),{'high','low'})
It's also nice to show the user the string that caused the error rather than just telling the user that there's an error. To show the user the string that caused the error, see my answer.
Lastly, I ran these two lines (ismember(lower(Type),options); strcmpi(Type,options);) 1,000,000 times and timed each iteration. On average, the strcmpi method is 7.5 times faster (p<0.0001, wilcoxon ranked-sum test).
madhan ravi
madhan ravi 2019 年 7 月 31 日
"If the user enteres "High" for example, it will cause an error."
That is exactly the intention of the OP.
Adam Danz
Adam Danz 2019 年 7 月 31 日
@madhan ravi, where is that stated in this thread? You may be assuming this is the intention of the OP but how do you justify your certainty ("exactly")?
'high' and 'low' appear to be binary flags and the conditional error is to alert the user that an alternative flag was entered. String inputs in matlab are typically not case sensitive. For example, in input parsing the default setting for case sensitivity is false. Other string inputs typically allow for case insensitivity, too (ie: sum([nan,1,2], 'OMITNAN')). It would make more sense for the OP to allow for case insensitivity to avoid potential user error when the user chooses to use upper camel case (aka pascal case). Surely the function should be able to interpret "High" and "Low".
madhan ravi
madhan ravi 2019 年 7 月 31 日
"How can I make sure that the variable only accept 'high' or 'low'? If anything else, then there will be an error like 'filter type error'. "
Adam Danz
Adam Danz 2019 年 7 月 31 日
編集済み: Adam Danz 2019 年 7 月 31 日
That doesn't mean the user wants to go against typical matlab syntax and force case sensitive input strings on what appears to be a binary flag.

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

その他の回答 (1 件)

Adam Danz
Adam Danz 2019 年 7 月 31 日
編集済み: Adam Danz 2019 年 7 月 31 日

0 投票

This solution uses strcmpi() which which compares strings, allowing for case insensitivity so "High" or "LOW" won't cause an error. When there is an error, it provides the "Type" string that caused the error.
function [Y] = butter2filtfilt(x, Fs, Fcutoff, Type)
options = ["high", "low"];
if ~any(strcmpi(Type, options))
error('Filter type error. ''%s'' not recognized.', Type)
end
end

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by