Plotting inside Matlab Function Block for real time signals in Simulink - matlab

I have a simulation running on Simulink and output signals change during simulation. I want to plot them at every step. What I can do is to use to Workspace blocks to transfer them to Matlab, but then I can only plot after the simulation finishes. I would like to plot the value at every instant of the simulation.
What I tried:
Create a figure in advance as: figure(1) and plot a static graph on it. Then I use
Matlab function inside Simulink :
function fcn(x,y)
coder.extrinsic('plot')
plot(x,y,'s','Markersize',8,'MarkerFaceColor','g','erasemode','background')
Where x and y are my signals as input to matlab function block. However this results in plotting x and y in every timestep, but I would like to plot only the last value of the signal on the figure and delete the previous ones, in other words refresh the plot so that it is going to act as an animation. How can I achieve that? Thanks in advance

I think your code should work, with a few minor modifications:
I would do the following if I were you:
In the model callbacks, define your figure in the InitFcn callback:
fig_h = figure;
ax_h = axes;
set(ax_h,'Xlim',[0 12],'YLim',[0 12]) % or whatever axes limits you want
Then in your MATLAB Function block:
function fcn(x,y)
%#codegen
coder.extrinsic('plot')
plot(x,y,'s','Markersize',8,'MarkerFaceColor','g','erasemode','background')
set(gca,'XLim',[0 12],'Ylim',[0 12]) % or whatever axes limits you want

You need little more elaborate function than just calling plot to animate your data. You should create a plot_fcn and make that function extrinsic. An example implementation of plot_fcn assuming scalar inputs with range 0 to 100 is
function plot_fcn(x,y)
persistent f h
if isempty(f)
f = figure;
h = plot(x,y,'s','Markersize',8,'MarkerFaceColor','g','erasemode','background');
axis([0 100 0 100]);
axis manual
end
figure(f);
set(h, 'XData', x);
set(h, 'YData', y);
You can then call this function as
function fcn(x,y)
coder.extrinsic('plot_fcn')
plot_fcn(x,y);
Also checkout other questions regarding animation in MATLAB plots.

Related

Plotting cdf and regular graph in same axis in matlab

I have a vector with 1000 random numbers called v. I also have a vector, called x that represents the domain of which the numbers in v are generated, and another vector y that has the numbers of the cdf of the values in v. I know that I can do plot(x,y); and get a smooth function of the (non-empirical) cdf, and I also know that I can do cdfplot(v) to get a function of the empirical cdf.
My question is: How can I get these plots on the same set of axis?
Thank you for your help.
You could either generate data for an empirical cdf plot using ecdf or plot it directly with cdfplot like you mentioned. I would recommend using cdfplot since it sets up a few more things, such as a grid:
hFig = figure;
cdfplot(v);
hold all;
plot(x, y);
And as a bonus! Consider showing the X axis in logarithmic units, whichever reveals the data the best for you:
hAxes = get(hFig, 'CurrentAxes');
set(hAxes, 'XScale', 'log')

Matlab, figures and for loops

I am trying to plot the following simple function; $y=A.*x$ with different A parameter values i.e. A=0,1,2,3 all on the same figure. I know how to plot simple functions i.e. $y=x$ by setting up x as a linspace vector so defining x=linspace(0,10,100); and I know that one can use the hold command.
I thought that one could simply use a for loop, but the problem then is getting a plot of all the permutations on one figure, i.e. I want a plot of y=t,2*t,3*t,4*t on the same figure. My attempt is as follows:
x=linspace(0,10,100);
%Simple example
Y=x;
figure;
plot(Y);
%Extension
B=3;
F=B*x;
figure;
plot(F);
%Attempt a for loop
for A= [0,1,2,3]
G=A*x;
end
figure;
plot(G);
This is how I would plot your for loop example:
figure;
hold all;
for A=[0,1,2,3]
G=A*x;
plot(G);
end
figure creates a new figure. hold all means that subsequent plots will appear on the same figure (hold all will use different colours for each plot as opposed to hold on). Then we plot each iteration of G within the loop.
You can also do it without the loop. As with most things in Matlab, removing the loop should give improved performance.
figure;
A=[0,1,2,3];
G=x'*A;
plot(G);
G is the outer product of the two vectors x and A (with x having been transposed into a column vector). plot is used to plot the columns of the 100x4 matrix G.

For loop in matlab plot

Hello i am having some problems understading basic plotting in matlab.
I can understand why you would use a for loop when plotting data?
Can anybody explain this to me?
I am making a simple linear plot. Is there any reason this should be inside a loop
If you are making a simple plot there is virtually no reason to use a loop.
If you check doc plot you will find that plot can take some vectors as input, or even matrices for more interesting situations.
Example:
x=0:0.01:1;
y=sin(x);
plot(x,y)
No there is no need in Matlab to use a for loop for plotting. If you are looking for a simple linear plot your code could look like this:
x=1:100;
y=3*x+4;
plot(x,y)
As you see there is no for loop needed. Same goes for nearly all plots and visualization.
A possible reason to use a for loop to plot thing may be having several data to plot in a single matrix. Say you have two matrix Ax (MxN) and Ay (MxN) where N the length of each data and M is the amount of different data wanted to plot. For example like in this case N is 201 and M is 3:
% Create Ax and Ay
Ax=meshgrid(0:0.1:20,1:3);
Ay=zeros(size(Ax));
% Sinusoidals with different frequencies
for k=1:3
Ay(k,:)=sin(k.*Ax(k,:));
end
% create colours
colorVec = hsv(3);
% Plot
hold on
for k=1:3
plot(Ax(k,:),Ay(k,:),'Color',colorVec(k,:))
end
hold off
You get:

plotting symfun and a self created function on same graph matlab

i am trying to plot two function in matlab, the first one is of kinf symfun:
p = symfun(0, [m]);
p(m) = p(m)+Ck(k-3)*exp(m*(k-3)*complex(0, 2*pi/25));
here Ck is another symfun and k is a variable i pre-defined.
i want to plot it in the same graph with a function i created using the function mode:
function [x1] = xt_otot_q3( t)...
i cant make the xt_otot_q3 function a symfun because it involves if statements.
- i tried to create 2 vectors sampling the two functions and plotting them together with the plot function but for some reason the 'p' function vectors gets preatty grotesque giving me wierd output...
- i tried plotting them both using ezplot function but for some reason the sampled vector i got form xt_otot_q3 shows only as a straight line at 0.
any ideas how i should plot them together? to plot the xt_otot_q3 function i must create a vector if i try to plot it directly using ezplot it gives me the following eror:
>> ezplot(xt_otot_q3, [-10 10])
Error using xt_otot_q3 (line 2)
Not enough input arguments.
thanks in advance.
If I am understanding it properly, you have two functions p, and xt_otot_q3. You want to plot them together.
syms t;
func1 = xt_otot_q3(t);
ezplot(func1, [-10 10]);
# retain current graph, for new graph
hold on;
# symbolic function p
ezplot(p, [-10 10]);
I hope it helps.

Real time plot in MATLAB

I'm very new to MATLAB and I was trying to display a real time plot of some calculations. I have an N sized vector and I work with m values at a time (say m = N/4), so I want to plot the first m values and then as soon as the second m values are calculated have them replace the first plot.
My approach was as follows:
for i=1:N,
...
//compute m
...
plot(m);
end;
but it fails to update the plot in every loop and waits for all the loops to finish to plot the data. My question is: Should I use another function instead of plot or could I add some delay in each loop?
I think there must be a way I'm not aware of for updating the plot instead of re-plotting it every time.
As Edric mentioned, you'll definitely want to include a drawnow command after the call to plot to force an update of the graphics. However, there is a much more efficient and smoother method to animate plots that doesn't involve recreating the entire plot each time. You can simply initialize your plot, capture a handle to the plot object, then modify the properties of that object in your loop using the set command. Here's an example:
hLine = plot(nan); % Initialize a plot line (which isn't displayed yet
% because the values are NaN)
for i = 1:N % Loop N times
...
% Compute m here
...
set(hLine, 'YData', m); % Update the y data of the line
drawnow % Force the graphics to update immediately
end
In addition, before your loop and after the call to plot you can set a number of axes properties, like the axes limits, etc., if you want the axes to stay fixed and not change their appearance with each new vector m that is plotted.
You can add a call to DRAWNOW to force the plot to update. See the reference page. Note that DRAWNOW causes the graphics event queue to be flushed, which may cause callbacks etc. to be executed.