フィルターのクリア

Generate an Error when an input value contains a comma

2 ビュー (過去 30 日間)
Maarten
Maarten 2014 年 10 月 3 日
コメント済み: Image Analyst 2014 年 10 月 3 日
My question is: is it possible to generate an error pop-up in my UI when an input value contains a comma? For now I got an error pop-up when the input value is not a number by the following code:
if isnan(cv)
set(hObject, 'String', 0);
errordlg('Input must be a number','Error');
end
Currently I am copying this code to every variable I enter (which are a lot of variables). This makes the .m-file a little unclear/confusing to read. Is it also possible to set this code as a general code that applies for the whole programm?

採用された回答

Image Analyst
Image Analyst 2014 年 10 月 3 日
if ~isempty(strfind(cv, ','))
% cv contains a comma
else
% cv does not contain a comma
end
  2 件のコメント
Maarten
Maarten 2014 年 10 月 3 日
編集済み: Maarten 2014 年 10 月 3 日
Is it also possible to set this code as a general code that applies for the whole program, so that I don't have to copy this code to every variable?
Image Analyst
Image Analyst 2014 年 10 月 3 日
I don't know what you want to do. You could put it into a function and call it every time. If you just want to eliminate commas from the string, you could do it in one line in two ways:
s='adkfsa,lsdjksad,sdlj';
sNoComma = strrep(s, ',', [])
sNoComma = s(s~=',')

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by