Clear Filters
Clear Filters

How can I pass a Figure Handle to Matlab code embedded in Simulink, and capture that plot as an Image in the loop?

6 views (last 30 days)
Hi all,
I'm working on a Simulink model that updates a plot every time step of the model. The plotting is done within an embedded Matlab function. I can make this work for a generic figure that I call in the basic 'figure(1); plot(x, y)' sense, but ideally I'd like to be able to pass a figure handle to the embedded Matlab function, so that I can create the figure with specific properties when I initialise the model, then plot it to directly, but I get errors whenever I try. Are figure handles just completely unsupported by embedded Matlab functions or is there a way to do this? I'm sure this used to work back when figure handles were just doubles, but now they are objects Simulink doesn't seem able to do this anymore.
Further to this, I would then like to capture this plotted image as an image matrix for further processing. Usually I would do this with the 'getframe' function, but I get an error that says the function is unsupported for code generation. Does anyone have a workaround? Any help would be greatly appreciated.
Many thanks, Chris

Answers (2)

Nick Choi
Nick Choi on 17 Nov 2017
Edited: Nick Choi on 17 Nov 2017
If you are planning to generate code from the Simulink model, you will not be able to use the 'plot' function because it is not supported for code generation. The following documentation page contains the list of functions supported for C/C++ code generation:
According to this documentation page, passing function handles to extrinsic MATLAB functions is not supported for code generation:
If you want to perform post-simulation data processing, I would suggest logging the data from the model and plotting it from within the MATLAB environment.

Faiza Bukenya
Faiza Bukenya on 9 Dec 2017
try this out,it worked for me. F = getframe(a); % a is your figure currently displaying your image with the rectangle % vessel_pericyte = F.cdata; hold off figure,imshow(a);

Community Treasure Hunt

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

Start Hunting!