MATLAB: [SOLVED! – Two Errors: CAT arguments dimensions are not consistent – Assignment has fewer non-singleton rhs dimensions than non-singleton subscripts ]

horzcatMATLAB

Hi, I have this error problem while Matlab try running this function from a script (I've post only a part of it because the problem is here):
if binary(numseg).adfp==incrocio(1:length(binary(numseg).adfp));
x=[binary(numseg).firstpoint' ; midpoint];
varx=(binary(numseg).raggiofp/3)^2;
vary=varx;
elseif binary(numseg).adsp==incrocio(1:length(binary(numseg).adsp));
x=[binary(numseg).secondpoint' ; midpoint];
varx=(binary(numseg).raggiosp/3)^2;
vary=varx;
end
vartheta=Pstima(3,3,tstar);
P=[varx 0 0 ; 0 vary 0 ; 0 0 vartheta];
xstimawall=x;
Pstimawall=P;
Pstima(:,:,tstar)=P;
Pplot(:,:,tstar)=P;
Xstima(:,tstar) = x;
Xplot(:,tstar)=x;
Note that Pstima is a 3D Array of 3×3 matrix and tstar is a scalar index between 1 and length(Pstima)
– First error: If I try running the function I have this error:
??? Error using ==> horzcat CAT arguments dimensions are not consistent.
Error in ==> startekfreprocessing at 87 P=[varx , 0 , 0 ; 0 , vary , 0 ; 0 , 0 , vartheta];
The problem is in vartheta. The variable appears to be a scalar but if I print vartheta and P on screen(I haven't put the ";" in vartheta and P):
vartheta =
0.0100
P =
3.0044 0 0
0 3.0044 0
0 0 0.0100
vartheta(:,:,1) =
0.0100
vartheta(:,:,2) =
0.0100
Why Mathlab print vartheta first like a scalar and then like a 3D Array of 2 1×1 matrix???
– Second error: If I try to solve the problem in this way:
P=[varx , 0 , 0 ; 0 , vary , 0 ; 0 , 0 , vartheta(1,1)]; (%% horzcat with vartheta(1,1) instead of vartheta)
Mathlab gave me this error:
??? Assignment has fewer non-singleton rhs dimensions than non-singleton subscripts
Error in ==> startekfreprocessing at 91 Pstima(:,:,tstar)=P;
…and print this on the screen:
vartheta =
0.0100
P =
3.0044 0 0
0 3.0044 0
0 0 0.0100
vartheta(:,:,1) =
0.0100
vartheta(:,:,2) =
0.0100
P =
3.0044 0 0
0 3.0044 0
0 0 0.0100
Please help me! Thank you so much…

Best Answer

If Pstima is a 3D array where each plane is 3x3, and tstar is a scalar, then if you define vartheta according to
vartheta=Pstima(3,3,tstar);
vartheta will be one 3x3 plane out of Pstima. size() should report that, not 1x1. Are you sure you didn't redefine size()? What does this say
which -all size
whos vartheta