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 CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

产品


版本

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by