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 view (last 30 days)
[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);

Accepted Answer

Image Analyst
Image Analyst on 16 Sep 2022
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);

More Answers (1)

dpb
dpb on 16 Sep 2022
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.

Categories

Find more on Variables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by