i want to plot a 3d graph using surf in matlab. i know how to plot it just using surf:
k = 5;
n = 2^k-1;
[x,y,z] = sphere(n);
c = hadamard(2^k);
p=surf(x,y,z);
but i want to plot this in realtime, and i want to update the values using set.
I tired:set(p,"XData",Xvalue,"YData",Yvalue,"ZData",Zvalue); but its giving me errors. has anyone plotted using surf in realtime?
1) you can use the linkdata command or toolbar button (or even Tools -> Link from the plot window)
2) programmatically: you need to call the command 'refreshdata' to signal that new data is available:
%% Define the data
t=linspace(0,2*pi,40);
y=sin(t);
%% Create the plot and set teh datasources
h=plot(t,y)
set(h,'YDataSource','y')
set(h,'XDataSource','t')
%% Now update the data and the plot
pause
y=sin(2*t);
refreshdata
This shows it for the plot, but expect surf will behave the same.
Related
I am creating a UI on app designer and I want to plot a bode in my UI.axes.
This figure contains two plots (magnitude, phase) and what I want to do is to plot each plot in different ui.axes.
I've managed to plot only the magnitude bode and the phase bode using the following code :
clc;
clear all;
num = [2];
den = [conv([1 1], conv([1 1], [1 1]))];
sys = tf(num, den);
[mag, phase, freq] = bode(sys, {0.1, 100});
bodemag(sys, freq)
h = bodeplot(sys, freq);
setoptions(h,'MagVisible','off');
This code gives me these two seperate plots :
I am trying to insert these plots in two different ui axes in my app.
Does any one have an idea or another approach on how to insert the plots ?
NB : I've tried the following :-
Writing the code direcly into the app designer but it creates a pop up instead
Using the plot(app.UiAxes, ...., ....) function but I can't seem to make it work
Can you show your code from appdesigner with the app.UIAxes.
Which Matlab realease do you use ?
As i know subplots are not supported in a UIAxes in older versions. So you have to make two UIAxes or use newer version.
If you write your code directly to appdesigner you are creating an Axes. There is an difference between UIAxes and Axes object. If you want to use Axes you have to set more properties to make it display inside your UIFigure of your App.
You tried this ?
I don't know if the UIAxes supports bodemag function. This could make sense, if it doesn't work with UIAxes.
bode = bodemag(app.UIAxes,sys, freq);
If UIAxes doesn't support bodemag then you have to do it with normal axes and code the positioning of this axes.
This is a good example how to do it.: https://de.mathworks.com/help/matlab/creating_guis/polar-plotting-app-gui-in-app-designer.html
And this is the part that is required additional.
% Create polar axes and position it in pixels
app.Pax = polaraxes(app.UIFigure);
app.Pax.Units = 'pixels';
app.Pax.Position = [260 55 230 230];
I have a .pcd file of a terrain survey that I opened using this command:
% read point cloud
big_island_cloud = pcread('C:\Users\to\path\Desktop\big_island_5m.pcd');
pcshow(big_island_cloud)
Now I read from a .csv file all necessary columns using readtable in the following way and and verify that everything is fine file(1:3,:) and plot the result using stackedplot:
file = readtable('C:\Users\to\path\Desktop\EKF_USBL_CAM_Pose_Projected.csv');
file(1:3,:)
timeStamp = file(:,1);
pose_pose_position_x = file(:,4);
pose_pose_position_y = file(:,5);
stackedplot(file,{'time','field_pose_pose_position_x'});
and the following is what I obtain, pose_pose_position_x is going to be the trajectory I have to draw on top of the .pcd file:
How do I obtain the following output?:
Thank you for shedding light on this matter
Supposing that you have [x,y,z] for the data of your trajectories then you can simply superpose a plot with the use of hold on and plot3:
Based on the example in the MATLAB documentation :
numFaces = 600;
[x,y,z] = sphere(numFaces);
figure;
pcshow([x(:),y(:),z(:)]);hold on % keeps the first plot on screen
plot3([1,1,-1],[1,0.5,1],[1,-0.5,-1]) % adds the trajectory on the plot
title('Sphere with Default Color Map');
xlabel('X');
ylabel('Y');
zlabel('Z');
Result
Edit
If you only have [x,y] then plot will replace plot3
plot([1,1,-1],[1,0.5,1]) % adds the trajectory on the plot
Note that the trajectory will be drawn on the plane with z=0
I am in need of plotting a 2D spectrogram of a signal in Matlab. I need it for a printed assignment, hence the 3D image makes no sense. However, when the signal is plotted using Spectrogram it automatically produces a 3D plot of the signal.
My Code:
Dataset = 1; % Dataset to be analysed
N = 1024; % Window size
Beta = 12; % Kaiser window beta value (small = narrow main lope)
Overlap = 800; % Window overlap
Threshold = -150; % Minimum magnitude before threshold
spectrogram(Enclosure{Dataset}(1:end),kaiser(N,Beta),Overlap,2048,fs,'MinThreshold',Threshold,'yaxis');
which produces a graph that looks like this:
But it is seen from the top, and the graph is really showing this:
The reason why i need it to specifically be 2D (and why i don't settle with a screenshot) is because i am using Matlab2tikz to convert Matlab figures into Tikz figures in LaTex. with the 3D images i get figures of +100 Mb and 2D will reduce the size to <1Mb.
I don't know what version of Matlab you are using but in 2015a you should be able to get a handle to the figure with the 3D plot and change the view angle to 2D:
view(0,90);
I've also got an example of how you can make your own 2D plot from the outputs of spectrogram() using a similar method:
x = [0:0.01:100];
y = sin(5*x);
y = awgn(y,0.1);
[S,F,T,P] = spectrogram(y,200,0,length(y)*5,100);
[m,n] = size(P);
figure(2)
surf(F,T,zeros(n,m),P','EdgeColor','none')
view(0,90)
xlabel('Frequency')
ylabel('Time (s)')
The output looks like this:
Hopefully since there is no altitude information, the figure size might be smaller but I can't test that since I don't have Matlab2tikz.
One option is to capture whatever its plotted and then plot it as an image. You can do this using getframe
if you do
F=getframe(gca);
cla;
imshow(F.cdata);
You'll get exactly what you will be seeing before, but as an image.
However I think it defeats a bit the purpose of Matlab2Tikz, as the idea os that you have Tikz code describing your data...
You can try the following:
[~,F,T,ps]=spectrogram(Enclosure{Dataset}(1:end),kaiser(N,Beta),Overlap,2048,fs,'MinThreshold',Threshold,'yaxis').
% Output the spectrum in ps
imagesc(T,F,10*log10(ps))
% Generate a 2d image
view(270,90)
xlabel('Time [s]')
ylabel('Frequency [Hz]')
c=colorbar;
c.Label.String='Power [dB]';
% Extra setting to make the plot look like the spectrogram
Good luck
I am trying to create a smaller plot within a plot in MATLAB, for example like the image of this MATLAB File Exchange Upload.
There, two figures are created and then both of them are plotted in one figure.
My problem however is that I already have two MATLAB figures from earlier simulations and I need to embed one figure into the other one, i.e., one would be small and the other plot would be big but in the same graph. Could someone suggest an easy way to do this?
This can be done using the copyobj function. You'll need to copy the Axes object from one figure to the other:
f(1) = openfig('fig1.fig');
f(2) = openfig('fig2.fig');
ax(1) = get(f(1),'CurrentAxes'); % Save first axes handle
ax(2) = copyobj(get(f(2),'CurrentAxes'),f(1)); % Copy axes and save handle
Then you can move and resize both axes as you like, e.g.
set(ax(2),'Position', [0.6, 0.6, 0.2, 0.2]);
I am using streamslice command to visualize my flow. I want to add color depending on magnitude of velocities but there seems to be no function argument in streamslice to do so. The function is given as:
% x - x-coordinates
% y - y-coordinates
% u,v - vector volume data
h = streamslice(x,y,u,v)
The function produces this image
If you want to use streamslice, I can suggest something. It would need some tweaking to get it look like a cool figure, but it does the job, I guess. the idea is to combine a surf plot with the streamsilces plot.
Look at the result. I guess that with better a colormap and with some tricks getting the data handle of the streamsliceto change the line colour it could work nicely, speciall yin Matlab R2014b or higher.
CODE:
clear;clc;
load wind
% Use only a piece of this datasheet
x=x(:,:,5);
y=y(:,:,5);
u=u(:,:,5);
v=v(:,:,5);
mag=sqrt(u.^2+v.^2);
figure
hold on
surf(x,y,mag-max(mag(:)),'FaceColor','interp','Edgecolor','none')
colormap('hot')
streamslice(x,y,u,v)
axis([min(x(:)) max(x(:)) min(y(:)) max(y(:)) -max(mag(:)) 0])