Plotting a 3D data in Matlab - matlab

This could be simple question. However, I tried/searched extensively before posting this question.
I have struct called particle and it contains a variable particle(i).center, which is actually a coordinate variable. I am trying to plot those coordinates using plot3 something like,
hold on;
for i=1:np
plot3(particle(i).center, 'r+')
end
I receive an error message saying the following:
Error using plot3
Not enough input arguments.
I realize the error is that the variable is passed as 3x1 array instead of 3 comma separated variables. Can anyone suggest, how to plot 3D coordinates as in above case?

Your particle structure needs to have
particle(i).center.x
particle(i).center.y
particle(i).center.z
and then plot3(particle(i).center.x,particle(i).center.y,particle(i).center.z,...)

Related

Matlab timeseries plot error

I am having an issue when trying to plot these two timeseries against each other:
subplot(2,2,3),plot(wrm,Te);title('Speed-Torque curve');
xlabel('wrm [rad/s]');ylabel('Te[Nm]')
axis([-5 10 20 200]);
grid;
It comes up with the error of
Error using plot
A numeric or double convertible argument is expected
Error in timeseries/plot (line 163)
p = plot(ax,Time,Data,varargin{:});
I have tried changing the "to workspace" variables in simulink from 1x1 double timeseries to arrays and that seems to allow me to plot, but the plot for just Te become incorrect.
All other plots I create, I am unable to follow the format of plot(time,yvariable) as it gives the same error so I have been just using plot(yvariable) which has worked up until now.
Any help would be appreciated. Thanks!
From your question I don't understand exactly what you are trying to do. If you are trying to plot the data members of the two timeseries objects against each other, use
plot(wrm.Data, Te.Data);
If you are plotting the data members, you of course need to make sure these vectors are the same length. If they are not, you could use something like
Te2 = resample(Te2, wrm.Time);
If you want to plot both against time instead, use
plot(wrm); hold on; plot(Te);
Or, as I like to do:
wrm.plot(); hold on; Te.plot();

matlab scatter3 plot real and imaginary parts over frequency

I've got to vectors called ttre and ttim which contain real and imaginary data over a frequency (from 1 to 64). The fields are looking like this:
ttim 64x10100 single
ttre 64x10100 single
I can easily make a 2D scatter plot of a certain row by using the command
scatter(ttim(40,:),ttre(40,:))
Now, I would like to display all data in a 3D scatter plot where X=real values, Y=imaginary values and Z=[1...64]
I created an array for Z with the number 1 to 64 and copied it to make it the same size as the other variables, by:
z=(1:64)'
z=repmat(z,1,10100)
result:
z 64x10100 double
When I try to plo a 3D scatter plot now, I get the error "Vectors x,yu,z must be of the same size"...however, as far as I understand, they are of the same size.
>> scatter3(ttim,ttre,z)
Error using scatter3 (line 64)
X, Y and Z must be vectors of the same length.
I hope that someone could point me into the right direction here.
Kind regards
scatter3 needs points to plot, so x,yand z should be 1xN , where N is the amount of points your are plotting. I dont know what your data is, so unfortunately I can not help more. Maybe scatter3(ttim(:),ttre(:),z(:)) works, but I do not recommend it for the huge amount of data you have, it may crash your computer.
However, maybe z=1:64 is not the best option. It means that you will have 64 layers (like floors from a building) of scattered data, not sure if that's what you want.

Creating a 3D function in Matlab using meshgrid

This should be a really simple question, but for some reason I'm getting unreasonably confused and the Matlab documentation isn't helping.
Given a uniform grid of coordinates (x_i,y_j,z_k), I want to make a 3-dimensional array F in Matlab such that F(i,j,k)=f(x_i,y_j,z_k). The following is obviously incorrect:
x=linspace(-1,1,100) % uniform mesh on [-1,1]^3
[X,Y,Z]=meshgrid(x);
f=X.*Y.*sin(pi*Y.*Z) % for example
Do I need to use permute somewhere? I know that I could simply make a triple loop, but as we know that is slow.
Thanks!
Use ndgrid instead of meshgrid to avoid the unwanted permutation between first and second dimensions.
From the documentation (see also here):
MESHGRID is like NDGRID except that the order of the first two input
and output arguments are switched (i.e., [X,Y,Z] = MESHGRID(x,y,z)
produces the same result as [Y,X,Z] = NDGRID(y,x,z))

How to plot a 3D figure in matlab based on a function like f(x,y,z)=0?

How to plot a 3D figure in MATLAB based on a function like f(x,y,z)=0?
And this complicated function can not be written as z = f(x,y).
f(x,y,z)=sum(a.*exp(sv(:,1)-x).^2+sv(:,2)-y).^2+sv(:,3)-z).^2)-b=0
where a is a known vector, sv is a known matrix, b is a known value. x,y,z are three variables. How to draw this surface in 3D way in matlab?
I just solve this question by this tool from the Matlab File Exchange:
Ezimplot3: implicit 3D functions plotter
your function only contains 1D vectors( I am assuming they are of equal lengths), if summed it will give you a constant; therefore, there is really nothing to plot.

MATLAB : identifying the parameters corresponding to the loop variable

I have trying to plot solutions to coupled ODEs.I solve them using ODE15s solver and pass arguments using a for loop.
MATLAB code then returns a set of plots to me(using hold on).Now only one of the given graphs is appropriate to my theory.So,how do I identify the argument(that I had passed through the for loop) which corresponds to my chosen graph(I can pick out the graph visually)
Just use 'text' function to mark the arguments nearby the each graph.There is a very simple demo(a is the argument):
x=0:100;
figure;
hold on;
for a=1:10
plot(x,a*x);
text(100,a*100,sprintf('a=%s',num2str(a)));
end
Sorry for not pasting picture here because my reputation is not enough.