How can I fix this error - "Matrix dimensions must agree" "x{end,1}.AppName == aa1{selA,1}.Type"

1 visualizzazione (ultimi 30 giorni)
if Sign == 1
if appm==0
tt.AppName=aa1{selA,1}.Type;
tt.status='[On]';
tt.dataOn=i-7;
x{end+1,1}=tt;
clear tt;
appm=1;
end
SignStr = '[On]';
iCount = iCount + 1;
if ~isempty(r{selA,1})
r{selA,1} = r{selA,1} + 1;
else
r{selA,1} = 1;
end
else
if isempty(x)~=1 && x{end,1}.AppName == aa1{selA,1}.Type
% AppName=aa1{selA,1}.Type;
tt.AppName=aa1{selA,1}.Type;
tt.status='[Off]';
tt.dataOn=i-7;
x{end+1,1}=tt;
clear tt;
end

Risposte (2)

Image Analyst
Image Analyst il 24 Dic 2018
Evidently selA is not a single scalar number so you're not selecting a single cell, but an array of cells.
  2 Commenti
Guillaume
Guillaume il 24 Dic 2018
Modificato: Guillaume il 24 Dic 2018
Hum, no. If SelA wasn't scalar the error would be Expected one output from a curly brace or dot indexing expression, but there were xxx results.
aa1{selA,1}.Type would error even before matlab can carry out the comparison.
Image Analyst
Image Analyst il 24 Dic 2018
Well, just a guess since we can't debug it. But here is a solution guaranteed to solve it: Click here
It also helps to break things into simple, temporary variables:
v1 = x{end,1}.AppName
v2 = aa1{selA,1}.Type
theyMatch = v1 == v2
My current guess is that they are strings and he should be using strcmpi(v1, v2) instead of using ==.
theyMatch = strcmpi(v1, v2);
if theyMatch && ............

Accedi per commentare.


Guillaume
Guillaume il 24 Dic 2018
Modificato: Guillaume il 24 Dic 2018
x{end,1}.AppName and aa1{selA,1}.Type are not the same size and neither of them is scalar in the non-scalar dimension of the other. As the error message tells you "Matrix dimensions must agree" since == does an element by element comparison.
If you are comparing char vectors, the function to use is strcmp:
if ~isempty(x) && strcmp(x{end,1}.AppName), aa1{selA,1}.Type) %return true if both char vectors are the same
Otherwise, it may be that isequal may work, or it may be that you've got a bug somewhere that makes the two matrices a different size.
Note that:
if isempty(x)~=1
is simpler as
if ~isempty(x)

Community Treasure Hunt

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

Start Hunting!

Translated by