Undefined function 'eq' for input arguments of type 'cell'.

7 ビュー (過去 30 日間)
Abdo  Mohsen
Abdo Mohsen 2017 年 9 月 8 日
編集済み: James Tursa 2017 年 9 月 8 日
what's wrong with this code?
x= {'Apples','Oranges','Potatoes','Tomatoes'};
y = {50,20,36,28};
list = [];
for i=1 length(x)
if x(i) == 'Apples'
list(i) = y(i);
end
end

回答 (1 件)

James Tursa
James Tursa 2017 年 9 月 8 日
編集済み: James Tursa 2017 年 9 月 8 日
You could change your test to this if you want an exact match:
if isequal(x{i},'Apples')
x(i) is a cell, whereas x{i} is what is contained in the cell which is a character string. You could also use the functions "strcmp" or "strcmpi" for instance instead of "isequal".
That being said, I am not sure you will be building your "list" variable as you expect. What exactly do you want "list" to be after your code is done? Probably using some form of the function "ismember" might be a better choice instead of the loop you currently have.

カテゴリ

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