user1664484
user1664484

Reputation: 43

If statement in Matlab error

I have the following piece of code,

temp = cell(0,206);
for i = 1:numel(models)
    mod = models{i};
    for j = 2:size(raw_quant,1)
        if raw_quant{j,4} == mod
            temp(end+1,:) = raw_quant(j,:);
        end
    end
end

when I run it, I get the error

"Error using ==

Matrix dimensions must agree."

mod is just a string, and I ensured that raw_quant{j,4} are all strings.

Any ideas?

Thanks, L

Upvotes: 0

Views: 45

Answers (1)

Geoff
Geoff

Reputation: 1603

I get the same error by doing the following

'geoff'=='was here'

The == operator assumes that an attempt is being made to compare two equally sized arrays or matrices, and is not specific to strings or char arrays. As you said, models is a 62x1 cell array. Any element within that array is not guaranteed to be a scalar (or single character).

If you want to compare two strings, then I suggest you use the strcmp function

if strcmp(raw_quant{j,4},mod)==1
     temp(end+1,:) = raw_quant(j,:);
end

As well, reconsider naming your variable mod which is a built-in MATLAB function for modulus after division to avoid any (perhaps) future bugs in the code when you may try to use the mod function rather than than the mod variable.

Upvotes: 2

Related Questions