Simulink: Update figure in matlab function

13 次查看(过去 30 天)
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!

回答(1 个)

Fangjun Jiang
Fangjun Jiang 2024-1-12
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 个评论
Fangjun Jiang
Fangjun Jiang 2024-1-12
编辑:Fangjun Jiang 2024-1-12
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 2024-1-13
Yes i think that works!
Thank you very much.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 View and Analyze Simulation Results 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by