Graphic object handle array in embedded matlab function in Simulink in R2014a or newer

1 view (last 30 days)
For debugging, I have some plots of vectors in an embedded matlab function in Simulink. Up to Matlab R2013b, everything works just fine with the following minimum example code:
function fcn
%#minimum example for plot within for-loop of embedded matlab function
coder.extrinsic('delete');
coder.extrinsic('quiver');
coder.extrinsic('gobjects');
numSteps=4;
persistent hFig;
persistent hVector;
if isempty(hFig)
hFig = figure;
hold on;
hVector=zeros(numSteps,1);
hVector=gobjects(numSteps,1);
for i=1:numSteps
hVector(i) = quiver(0,0,0,0);
end
end
set(0, 'CurrentFigure', hFig);
delete(hVector);
for i=1:numSteps
startInc=[1 1;1 1].*i;
endInc=[2 3;2 -3].*i;
hVector(i) = quiver(startInc(1,:),startInc(2,:),endInc(1,:),endInc(2,:));
end
For the handle array `hVector`, an initialization is necessary due to its use in the `for`-loop. However, for an Initialization of a graphics handle object, the function `gobjects` is needed and in turn the initialization as double with `zeros(numSteps,1)` becomes necessary, since matlab cannot correctly determine the data type of an output of an extrinsic function. As I said, this code snippet works just fine if copied to an embedded matlab function block in simulink without anything else in the model.
My problem: Mathworks changed a lot of the plotting functions in R2014a, one of the changes being the datatype of the graphics handles which are no of type `quiver` for my quiver plot. Thus, the initialization with `zeros(numSteps,1)` initializes the wrong data type for the handle array and I get the following error message:
An error occurred while running the simulation and the simulation was terminated
MATLAB expression 'gobjects' is not numeric.
However leaving it out still does not work, because of the problem mentioned above. Neither does a init loop or anything similar compile without errors.
I'd greatly appreciate any help on that issue.

Answers (1)

Philip Caplan
Philip Caplan on 28 Apr 2015
Hi Stefan, you are correct. With the changes to the graphics system, you will need to allocate the "hVector" array differently. You can retrieve the version of MATLAB used and allocate it accordingly:
if verLessThan('matlab','8.4')
hVector = zeros(numSteps,1); % < R2014b
else
hVector = gobjects(numSteps,1); % >= R2014b
end
  1 Comment
Stefan
Stefan on 29 Apr 2015
Thanks for your answer. However, my problem was not determining the MATLAB version but that in an embedded matlab function in my Simulink model I cannot just call 'gobjects' for initialization. MATLAB requires me to define the type of the variable before, else I get an error when accessing the initialized handle vector the first time. The error message i "Subscripting into an mxArray is not supported." which I found in other posts to be a problem of extrinsic functions. It seems that MATLAB cannot determine the output data type of an extrinsic function, thus I would need to initialize. However, since my extrinsic function is the initialization function 'gobjects' I'm stuck at that point.

Sign in to comment.

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!