Error using horzcat. CAT arguments dimensions are not consistent.

5 views (last 30 days)
Dear all,
Using the next function, I get the error mentioned:
function [Y]=descriptiveStats( X )
M={};
N=[];
M={'mean','sem','numel','std','var','min','max'}.';
[mean,sem,numel,std,var,min,max]=grpstats(X,[],{'mean','sem','numel','std','var','min','max'});
N=[mean,sem,numel,std,var,min,max].';
Y=[M,N];
xlswrite('df.xlsx',Y)
end
The results displayed in the command window look like this:
>> dFStats = descriptiveStats(decodeFemale);
size(M)
7 1
size(N)
7 1
M
'mean'
'sem'
'numel'
'std'
'var'
'min'
'max'
N
1.7802
0.0935
186.0000
1.2749
1.6254
0
4.7137
Error using horzcat
CAT arguments dimensions are not consistent.
Error in descriptiveStats (line 20)
Y=[M,N];
However, if I use an additional argument gname that displays a character I don't get the aforementioned error:
function [Y]=descriptiveStats( X )
M={};
N=[];
M={'mean','sem','numel','gname','std','var','min','max'}.';
[mean,sem,numel,gname,std,var,min,max]=grpstats(X,[],{'mean','sem','numel','gname','std','var','min','max'});
N=[mean,sem,numel,gname,std,var,min,max].';
Y=[M,N];
xlswrite('df.xlsx',Y)
end
And this are the results printed in the command window:
>> dFStats = descriptiveStats(decodeFemale);
size(M)
8 1
size(N)
8 1
M
'mean'
'sem'
'numel'
'gname'
'std'
'var'
'min'
'max'
N
[1.7802]
[0.0935]
[ 186]
'1'
[1.2749]
[1.6254]
[ 0]
[4.7137]
It's not a big deal to have to use the gname argument in order to avoid the error, but I don't understand why it is happening.
Can somebody explain what's the cause and how to fix it?
Regards,
Diego

Accepted Answer

Walter Roberson
Walter Roberson on 5 Dec 2012
Easy. Without the gname argument, what is being returned is a simple 7 x 1 numeric array, which would occupy at most one cell entry rather than the 7 you would need in order to match the 7 x 1 cell array of strings you constructed in M. But when you use gname, it needs to return a string as part of the output and so cannot just return a numeric array, so it returns a cell array of the right size for you to match with M.
Solution:
[M, num2cell(N)]

More Answers (2)

Vishal Rane
Vishal Rane on 5 Dec 2012
Check the data type of N in both cases.

Ganesh K Davanagege
Ganesh K Davanagege on 25 Nov 2016
Edited: Ganesh K Davanagege on 25 Nov 2016
I have the same problem with horzcat. I tried num2cell. But, it is not working. Can anybody help me.
clc
clear all
filename = 'testdata.xlsx';
timeVariable=ones(1,10)
timeVariable = timeVariable'
tempVariable = 10*rand(1,10)
tempVariable = tempVariable'
% A = {'Time','Temperature'; 12,98; 13,99; 14,97};
a = horzcat(timeVariable, tempVariable);
A = {'Time','Temperature'; [timeVariable, tempVariable]};
% A = {'Time','Temperature'; a}; % I tried this also.
sheet = 1;
xlRange = 'E1';
xlswrite(filename,A,sheet,xlRange)

Community Treasure Hunt

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

Start Hunting!