Simulink: Update figure in matlab function

17 views (last 30 days)
Gabriel
Gabriel on 12 Jan 2024
Commented: Gabriel on 13 Jan 2024
Hello,
i am trying to update a figure which is created in the InitFcn Callback, with another Matlab function in Simulink.
My InitFcn Callback runs a m file in which the figure is created and a first plot is made. I save this plot to a global variable.
During the simulation the x,y,z data of my plot is updated and i want do draw it in real time. So i tried to update the plot command with "set" command in another matlab function.
Background: It's a 6-DOF (Stewart) platform for a flight simulator and i want to draw the position of the platform in this figure. The position is read from a flight simulator program.
The Problem: I don't know how to update the plot in another matlab function. Or basically i don't know how to make an variable (which i created in the InitFcn m file) in another matlab function accesable.
figure_init.m creates the figure and make a first plot:
and in this function the plot should be updated:
Thank you for your suggestions!

Answers (1)

Fangjun Jiang
Fangjun Jiang on 12 Jan 2024
This could work. The key is to declare a global variable in both the figure_init() function and update_plot() MATLAB Function block. You need to turn on hold, not sure how the figure would be refreshed though during simulation.
global fig;
fig=figure(1); hold on;
What I think you really need is this.
  6 Comments
Fangjun Jiang
Fangjun Jiang on 12 Jan 2024
Edited: Fangjun Jiang on 12 Jan 2024
It works. No need to use global variable. Just make sure all figures are closed before model simulation. If you really want to make it robust, I think you can use global variable to pass the figure number. In that case, you don't need to close or worry about other figures. Fig1=findobj(0,'type','figure','Number',GlobalFigureNumber);
%figure_init
figure(1);plot(1:3);xlim([1 10]);ylim([1 10]);
function y = update_plot(u)
Fig1=findobj(0,'type','figure');
Line1=findobj(Fig1,'type','line');
set(Line1,'XData',1:u,'YData',1:u);
y = u;
u is a digital clock thus its value is 1 at time 1, 2 at time 2, ... I see the figure line increasing as the simulation runs.
Gabriel
Gabriel on 13 Jan 2024
Yes i think that works!
Thank you very much.

Sign in to comment.

Categories

Find more on View and Analyze Simulation Results in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!