If condition with a cell include both number string and text string?

2 ビュー (過去 30 日間)
Khanh
Khanh 2014 年 10 月 21 日
コメント済み: Khanh 2014 年 10 月 22 日
Hi everyone,
I have a cell:
mycell ={'text','-1','0','','textnumber01234','3123','0.111'}
I'm trying using if condition for showing a error window with each value of each cell element except two last elements ('3123','0.111'). These string elements which are converted to number by str2double have values greater than 0.
mycell={'text','-1','0','','text-with-number01234','3123','0.111'}
for ii=1:length(mycell)
if strcmp(mycell(ii),'') || str2num(mycell(ii))<=0 || isempty(mycell(ii))
% --- FIGURE -------------------------------------
figure1= figure( ...
'Tag', 'figure1', ...
'Units', 'pixels', ...
'Position', [515 655 310 90], ...
'Name', 'Wrong format data', ...
'MenuBar', 'none', ...
'NumberTitle', 'off', ...
'Color', [0.941 0.941 0.941]);
% --- STATIC TEXTS -------------------------------------
uicontrol( ...
'Parent', figure1, ...
'Tag', 'text1', ...
'Style', 'text', ...
'Units', 'pixels', ...
'Position', [25 17 250 50], ...
'FontSize', 10, ...
'String', {'Please enter right format data'}, ...
'HorizontalAlignment', 'left');
return
end
end
My code doesn't work properly.
Could someone help me?
Thanks.

採用された回答

Robert Cumming
Robert Cumming 2014 年 10 月 21 日
編集済み: Robert Cumming 2014 年 10 月 21 日
The problem here is the use of str2num in your conversion and the fact one of your inputs is text (which is a matlab function name which returns a handle), try:
str2num ( 'text' )
the result may surprise you....
the quirky result is because str2num uses eval. - str2double is generally a much better solution, with that in mind see below:
mycell={'text','-1','0','','text-with-number01234','3123','0.111'};
index = cellfun ( @str2double, mycell );
flags = ~(isnan(index) + index>0);
for ii=1:length(mycell)
if flags(ii)
% --- FIGURE -------------------------------------
figure1= figure( ...
'Tag', 'figure1', ...
'Units', 'pixels', ...
'Position', [515 655 310 90], ...
'Name', 'Wrong format data', ...
'MenuBar', 'none', ...
'NumberTitle', 'off', ...
'Color', [0.941 0.941 0.941]);
% --- STATIC TEXTS -------------------------------------
uicontrol( ...
'Parent', figure1, ...
'Tag', 'text1', ...
'Style', 'text', ...
'Units', 'pixels', ...
'Position', [25 17 250 50], ...
'FontSize', 10, ...
'String', {'Please enter right format data'}, ...
'HorizontalAlignment', 'left');
return
end
end
  1 件のコメント
Khanh
Khanh 2014 年 10 月 22 日
Great. Thank you so much. It works so nice.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by