How can I check if it is a string?

11 ビュー (過去 30 日間)
Berkin Tan
Berkin Tan 2022 年 1 月 3 日
コメント済み: Berkin Tan 2022 年 1 月 5 日
x = str2double(get(handles.demandValue,'String'));
y = str2double(get(handles.generationValue,'String'));
z = x - y;
set(handles.generationBUS2,'String',num2str(z));
tf = isString(x)
if tf == 1
f = errordlg('Dont write anything except number!','Error!');
return
end
Hi,
I have created a small GUIDE project on my own and I want to check if the user input contains any words and stop the execution. I have tried to write the tf = isStringpart but the tf always returns 0. x and y variables are created on static text.
Thanks.
  2 件のコメント
Matt J
Matt J 2022 年 1 月 3 日
Because x is the output of str2double(), you know it will always be a double, and therefore it will never be a string.
Berkin Tan
Berkin Tan 2022 年 1 月 3 日
Thanks.

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

採用された回答

Stephen23
Stephen23 2022 年 1 月 3 日
編集済み: Stephen23 2022 年 1 月 3 日
The simplest approach is to use ISNAN, because STR2DOUBLE will return NaN for any input that it cannot convert to numeric:
if isnan(x) || isnan(y)
  1 件のコメント
Berkin Tan
Berkin Tan 2022 年 1 月 5 日
Thanks for the answer.

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2022 年 1 月 3 日
Try the lettersPattern() function:
pat = lettersPattern creates a pattern that matches text composed of one or more of letters. lettersPattern is Unicode tolerant.
or digitsPattern():
pat = digitsPattern creates a pattern that matches text composed of one or more digit characters. digitsPattern is Unicode tolerant.
Using those (there are other pattern functions by the way), you can determine whether the user entered letters, numerical digits, or a combination of them into the edit text box.

カテゴリ

Help Center および 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