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

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

回答(1 个)

Philip Caplan
Philip Caplan 2015-4-28
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 个评论
Stefan
Stefan 2015-4-29
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Graphics Object Programming 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by