How can I make a 3d

Hello everyone, I have had problems when making a 3d plot for different rectangular pulses, in the 'y' axis the amplitude should go in voltage, in the 'x' axis the time, finally in the z axis the samples. I have searched for information but have not been able to find a good source.

Answers (1)

There are several ways to do that. One is to use the plot3 funciton, another is the waterfall function.
t = linspace(0,5);
s = sin((1:6).'*2*pi*t/5);
figure
hold on
for k = 1:size(s,1)
plot3(t, ones(size(t))*5*k, s(k,:))
end
hold off
grid on
view(50,50)
Experiment with your own data.
.

9 Comments

My code only plots data.
The ‘s’ variable is a matrix, and my code plots each row, since that is how I wrote it.
Your data has a ‘Time’ column vector and a ‘Voltaje’ column vector, so to plot it in 3D, the code would be something like this:
figure
plot3(Time, ones(size(Time)), Voltaje)
grid on
view(50,50)
Make appropriate changes to get the result you want.
If you have a ‘Voltaje’ matrix with each signal a column vector, my code would change to:
figure
hold on
for k = 1:size(s,1)
plot3(Time, ones(size(Time))*5*k, Voltaje(:,k))
end
hold off
grid on
view(50,50)
This would plot the columns individually, similar to what my code plots. Note that this version addresses the columns of the ‘Voltaje’ vector rather than the rows of my original code.
.
David
David on 26 Jun 2022
OK thank you. And how can I leave the graphs separated by a unit on the z axis? Separated as samples, for example, I have 4 graphs and I want them to be separated by a unit on the z axis.
These are separated on the y-axis, and they are separated arbitrarily by 5 units. That is the purpose of the ones call in the plot loop. (Separating them on the z-axis would separate them vertically, however that is not what the original plot in your question demonstrated.) The position of the ones call in the plot3 call argument list controls the axis on whcih they are separated, so experiment to get the result you want.
You will need to do something like this —
Timem = cat(2,Time12, Time2,Time3,Time4); % Concatenate To Matrix
Voltajem = cat(2, Voltaje1,Voltaje2,Voltaje3,Voltaje4); % Concatenate To Matrix
figure
hold on
for k = 1:size(s,1)
plot3(Timem(:,k), ones(size(Timem,1))*k, Voltajem(:,k))
end
hold off
grid on
xlabel('Time (ns)')
ylabel('Sample')
zlabel('Ampliitude (a.u.)')
view(50,50)
That should get you started.
See the documentation on plot3 for details.
.
David
David on 26 Jun 2022
OK thank you. What I don't understand is that I am not using the variable 's' at any time, in your code this variable corresponded to a sine that varied with respect to time, but I am not using that sine.
I created the ‘s’ matrix to illustrate the approach, since I do not have your data.
Use the code I previously posted instead:
Timem = cat(2,Time1,Time2,Time3,Time4); % Concatenate To Matrix
Voltajem = cat(2, Voltaje1,Voltaje2,Voltaje3,Voltaje4); % Concatenate To Matrix
figure
hold on
for k = 1:size(s,1)
plot3(Timem(:,k), ones(size(Timem,1))*k, Voltajem(:,k))
end
hold off
grid on
xlabel('Time (ns)')
ylabel('Sample')
zlabel('Ampliitude (a.u.)')
view(50,50)
Before you run it, be sure that all the appropriate ‘Time’ and ‘Voltaje’ vectors are in your workspace, and defined above (before) my code in your script.
.
I have no idea what you are doing.
I got the impression that you have 4 (100000x1) vectors each for the independent and dependent variables. My code concatenates them into 2 (100000x4) matrices. That should be well within the ability of your computer to do.
Instead, you appear to be multiplying one of the vectors by its transpose.
I am using exactly the same code:
t = linspace(0,5);
s = sin((1:6).'*2*pi*t/5);
Timem = cat(2,Time1,Time2,Time3,Time4); % Concatenate To Matrix
Voltajem = cat(2, Voltaje1,Voltaje2,Voltaje3,Voltaje4); % Concatenate To Matrix
figure
hold on
for k = 1:size(s,1)
plot3(Timem(:,k), ones(size(Timem,1))*k, Voltajem(:,k))
end
hold off
grid on
xlabel('Time (ns)')
ylabel('Sample')
zlabel('Ampliitude (a.u.)')
view(50,50)
That demonstration code is not appropriate for your data.
Use this instead:
Timem = cat(2,Time1,Time2,Time3,Time4); % Concatenate To Matrix
Voltajem = cat(2, Voltaje1,Voltaje2,Voltaje3,Voltaje4); % Concatenate To Matrix
figure
hold on
for k = 1:size(s,1)
plot3(Timem(:,k), ones(size(Timem,1))*k, Voltajem(:,k))
end
hold off
grid on
xlabel('Time (ns)')
ylabel('Sample')
zlabel('Ampliitude (a.u.)')
view(50,50)
.

Sign in to comment.

Categories

Products

Release

R2021a

Tags

Asked:

on 25 Jun 2022

Commented:

on 26 Jun 2022

Community Treasure Hunt

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

Start Hunting!