Strings cannot be compared

After I enter in the strings into the textboxes, they will not be compared. The errors said 'Error using == Matrix dimensions must agree.' Can someone help me fix this problem. Here is my following code. Thank you.
function pushbutton1_Callback(hObject, eventdata, handles)
x = get(handles.edit1,'String');
y = get(handles.edit4,'String');
if(x == 'Rice')
if(y == 'Noodle')
strcmp(x, y);
set(handles.edit2,'String','Eat rice for lunch');
end
end
if(x == 'Swimming')
if(y == 'Bowling')
strcmp(x, y);
set(handles.edit2,'String','Let's go swimming');
end
end

回答 (2 件)

Walter Roberson
Walter Roberson 2017 年 1 月 30 日

0 投票

Those are not strings, those are character vectors. Strings were added in R2016b. To compare character vectors that might be of different length use strcmp()

2 件のコメント

Guillaume
Guillaume 2017 年 1 月 30 日
編集済み: Guillaume 2017 年 1 月 30 日
The addition of the string class in matlab is very welcomed, but it does mean that we have to be a lot more careful about how we name char arrays. Calling a char array a string before R2016b was perfectly fine.
Walter Roberson
Walter Roberson 2017 年 1 月 30 日
I wish they had used a different word, but I am not sure what other word they might have used.

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

Guillaume
Guillaume 2017 年 1 月 30 日

0 投票

You seem to sort of know that you need to use strcmp for comparing string since you've put a (useless in this case) strcmp inside your if. When comparing character vectors, you must always use strcmp
if strcmp(x, 'Rice') & strcmp(y, 'Noodle')
set(handles.edit2,'String','Eat rice for lunch');
end
Or, since R2016b, you could use the new string class, where comparison is a lot more intuitive since it uses ==
if string(x) == 'Rice' & string(y) == 'Noodle'
set(handles.edit2,'String','Eat rice for lunch');
end

カテゴリ

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

質問済み:

2017 年 1 月 30 日

編集済み:

2017 年 1 月 30 日

Community Treasure Hunt

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

Start Hunting!

Translated by