I am receiving error for the following code , but I can't understand where and what to change I am having error at 5th line

1 回表示 (過去 30 日間)
[num,txt,raw]= xlsread('teamdata.xlsx');
disp(raw);
for i = 1:6
for j = 1:2
if raw{i,j} == 'URVASHI'
raw{i,1}= 8;
raw{i,j} = 'GHANGARE';
end
end
end
disp(raw);

採用された回答

Image Analyst
Image Analyst 2022 年 9 月 16 日
Don't compare character arrays with ==. Use strcmpi
[num,txt,raw]= xlsread('teamdata.xlsx');
disp(raw);
for i = 1:6
for j = 1:2
if strcmpi(raw{i,j}, 'URVASHI');
raw{i,1}= 8;
raw{i,j} = 'GHANGARE';
end
end
end
disp(raw);

その他の回答 (1 件)

dpb
dpb 2022 年 9 月 16 日
You forgot to attach the error message in context or to even give us a klew what raw contents are...but, taking a guess as to what could easily go wrong here, I'll posit an example--
raw={'STRING'}; % set a proposed value for the cell content
raw{1} == 'URVASHI' % try a direct equality comparison
would be my guess as the most probable error; there are a myriad of other things that could go wrong, but that's the most likely.
"You can't do that!!!"
To do string comparisons, use
if strcmp(raw{i,j},'URVASHI')
But, in your code snippet, that aside, it doesn't make sense -- you compare both the first and second columns to contain a magic string, but if it were to do so, you then change the first column to a number, but immediately overwrite that with a different string. This code, if it were fixed as above, would end up with both columns 1 and 2 for the rows which contained the target string being 'GHANGARE'. I'm guessing that's probably not what was intended, but we don't know that since the objective wasn't described and it's hard to guess without knowing what's in the file to begin with.
for i = 1:6
for j = 1:2
if raw{i,j} == 'URVASHI'
raw{i,1}= 8;
raw{i,j} = 'GHANGARE';
end
end
end
disp(raw);
Also, xlsread is deprecated; it would probably be easier to write the code if were to use readtable instead as is recommended.

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

製品


リリース

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by