Search code examples
matlabstructdimensions

MATLAB: Error using * Inner matrix dimensions must agree


So I'm working on a function that will receive inputs from from a user-defined structure to plot an ellipsoid, but Matlab keeps spitting out this error. Here's the portion I'm having trouble with:

theta = 0:(0.1):2*pi;
phi = 0:(0.1):pi;
a1 = ellipsoid_in(1).major_axis;
b1 = ellipsoid_in(1).minor_axis;
c1 = ellipsoid_in(1).transverse_axis;

x1 = a1*sin(phi)*cos(theta);
y1 = b1*sin(phi)*sin(theta);
w1 = c1*cos(phi);
plot3(x1,y1,w1)
grid on
hold on
x2 = x1;
y2 = y1;
w2 = w1;
plot3(x2,y2,z2)
xx = [x1;x2];
yy = [y1;y2];
ww = [w1;w2];

The error is occurring in my first (x1) equation and I've already tried using the .* operator on them all with the same result. I'm guessing the problem is coming from the 1x2 structure I'm calling, but I don't know how to fix it. The variables for the structures all correspond to scalars. Any help is much appreciated.


Solution

  • theta = 0:(0.1):2*pi;
    phi = 0:(0.1):pi;
    

    With the above two lines, you've created two vectors. These are different lengths (since one goes to pi and the other to 2*pi, by the same step size.

    You do want to use element-wise multiplication (.*), but you need your vectors to be the same lengths... otherwise, which elements get multiplied together?