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

1 回表示 (過去 30 日間)
Mohammad Junayed
Mohammad Junayed 2018 年 12 月 14 日
編集済み: Guillaume 2018 年 12 月 24 日
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
  1 件のコメント
Guillaume
Guillaume 2018 年 12 月 24 日
Please use proper indenting in your code as it makes it much more readable.
Comments are missing from your code.

サインインしてコメントする。

回答 (2 件)

Image Analyst
Image Analyst 2018 年 12 月 24 日
Evidently selA is not a single scalar number so you're not selecting a single cell, but an array of cells.
  2 件のコメント
Guillaume
Guillaume 2018 年 12 月 24 日
編集済み: Guillaume 2018 年 12 月 24 日
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 2018 年 12 月 24 日
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 && ............

サインインしてコメントする。


Guillaume
Guillaume 2018 年 12 月 24 日
編集済み: Guillaume 2018 年 12 月 24 日
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)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by