Matrix dimensions must agree problem

Hi, everyone! when I try to run my script they always tell me: Matrix dimensions must agree. I want to compare two matrices full with data imported from some XLS files. this is the script:
if true
clear
close all
%C = cell(1,n); % n is the total of csv files
C = cell(1,6);
D = cell(1,6); %
for k = 1:6 %n
name = sprintf('%04i.csv',k); %I choose the notation 000k to make it easy on us to upload all the data
C{k} = xlsread(name);
%figure(k),subplot(1,1,1),histogram(C{k}) %plot the histogram of each file
end
for i = 2:6
D{i}=C{i}==C{i-1}; %the D cell is the cell where I put the diffrences between the C cells
end
%A = cat(3,C{:}); %A is the matrix of all files
%figure(1),subplot(1,1,1),histogram(A) %plot the histogram of all files
%%n+1
end

7 commentaires

KSSV
KSSV le 21 Sep 2017
Which line number throws error?
Walter Roberson
Walter Roberson le 21 Sep 2017
Your code assumes the files are all exactly the same number of numeric entries.
Matrix dimensions must agree.
Error in ll (line 12)
D{i-1}=C{i}==C{i-1};
those lines. Yeah because those files are based on some thermal images so the lines and columns are basically some pixels (they don't change)
Before that line, add the line
assert(size(C{i}) == size(C{i-1}), 'Files are different sizes');
Thanks for your proposition but unfortunately it is not working :
Error using assert
The condition input argument must be a scalar logical.
Error in ll (line 12)
assert(size(C{i}) == size(C{i-1}), 'Files are different sizes');
assert(isequal(size(C{i}), size(C{i-1})), ''Files are different sizes');
would work. Note that it won't fix the problem. It will just confirm whether or not the files are the same size.
I would recommend you use better variable names. Use full words that describe what's in the variable (e.g. thermalimages instead of C, imagedifferences instead of D, etc.). It makes your code instantly easier to understand and therefore easier to debug.
fatima-zahra achbah
fatima-zahra achbah le 21 Sep 2017
Oh, thanks it works now, apparently the problem was with my first file it was empty. hahaha joke on me. Thank you all guys I appreciate your help

Connectez-vous pour commenter.

Réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by