フィルターのクリア

If statement using Char class variable

5 ビュー (過去 30 日間)
Diego Tasso
Diego Tasso 2012 年 6 月 19 日
Hi there! I have had a cell class variable text3 size 1X2 containg '11-03'. I used the cell2mat function to convert this variable to char class variable cause I want to use an IF statement however the size of the variable remains 1X2. Below is an example of what I am looking to do:
text3 = { '11-05', '' , '' , '' ;
'' 'X' 'Y' 'Contact Size';
'A' '' '' '' ;
'B' '' '' '' ;
'C' '' '' '';
'D' '' '' '' ;
'E' '' '' '' };
[matchobj strsplit] = regexp(text3,'11','match','split');
A = cell2mat(matchobj{1:1});
if A < 15
P = 6;
else
P = 5;
end
Any one have any ideas as to what I can do to fix this ? Everything compiles.
  1 件のコメント
Diego Tasso
Diego Tasso 2012 年 6 月 19 日
By the way, A returns with 11 so I am thinking that its the fact that the size remains the same as when it was a cell class variable that is messing up the if statement.

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

採用された回答

Walter Roberson
Walter Roberson 2012 年 6 月 19 日
regexp() returns a cell array of strings for 'match'. matchobj{1:1} is thus already a string, so cell2mat() is just leaving it alone as a string. You then try to compare that string to the numeric constant 15. The string in the first case is '11' which is char([49 49]). [49 49] < 15 is false, so P = 5 will be assigned. The size() of '11' is 1x2 .
If you are wanting to interpret the '11' as a hex number and compare the decimal result to 15, then
A = hex2dec(matchobj{1});
  1 件のコメント
Diego Tasso
Diego Tasso 2012 年 6 月 19 日
Thanks once again. It all makes sense know.

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

その他の回答 (0 件)

カテゴリ

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