MATLAB: Transpose… Not Transposing!

MATLABtranspose

Hello Community,
I have an annoying glitch in some code, in that there is a line in my script that tells some data that has just been created in a for loop and stored to a struct to transpose. However, sometimes it will transpose and the subsequent output from the script works, and other times it doesn't transpose, and I get thrown a "matrix dimensions must agree" error! The code is:
% Calculate mean for all VLM CA ranges in the 'ZS.' struct
for i = 1:N
ZS.vlmcamean(i) = mean(ZS.ZSFLCA.(sprintf('ZSca%d',i)));
end
% Transpose the VLM CA mean ('vlmcamean') field
ZS.vlmcamean = ZS.vlmcamean';
Where you can clearly see the crucial " ZS.vlmcamean' " to transpose the data. The reason I do this is to make the data be the correct size for a function that follows in the script.
Can anyone suggest a workaround or advise on how to have this transpose element consistently do what it is meant to?
Many thanks.
10B.

Best Answer

Please show the entire error message - all the red text, not just part of it. I find it hard to believe the
ZS.vlmcamean = ZS.vlmcamean';
would throw an error. It's just simply transposing a row vector into a column vector. Let me see the entire error message. Also put these line before that line
rowVector = ZS.vlmcamean;
whos rowVector
The workaround is to just use reshape()
ZS.vlmcamean = reshape(ZS.vlmcamean, [], 1); % Make into column vector.
Related Question