Simulink: Update figure in matlab function

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일

0 개 추천

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

Gabriel
Gabriel 2024년 1월 12일
Unfortunately I don't think this works. Because for global variables to access i need a Data store memory block. Without this i cannot access the variable in the function.
And here is the problem: The initial value of the data store memory block cannot be a figure handle (fig). I get an error. However there is a field for the data type and i don't think there is the data type assigned to a figure handle.
Regards,
Ok, I see. Not my recommended approach anyway. Thought it could work. What about just ploting to figure one, figure(1)?
Gabriel
Gabriel 2024년 1월 12일
Plot command is not a very time efficient solution. It's very slow...
not practical for real time drawing.
That was my concern too. But in terms of making it work, would making the global variable passing just a number work? What I meant was. It used to be this way. fig=figure(1). "fig" would be just the numerical number 1. Now it is a figure handle and its "value" is 1. In your update_plot() function, search the axes or line in figure 1, then use set() to update. Would that work?
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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Graphics Performance에 대해 자세히 알아보기

질문:

2024년 1월 12일

댓글:

2024년 1월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by