Scalar variable as output of Matlab function in Simulink
Ältere Kommentare anzeigen
I careted a Simulink model in MATLAB 2017 for debug. The model includes only a MATLAB function. It is shown in the image below;

The code in the MATLAB function looks like below:
function y = fcn(time,stateVariables)
ii = find(stateVariables(:,1)<=time,1,'last');
if isempty(ii)
ii = 1;
end
y = ii
end
The stateVariables was loaded in the base workspace, as a parameter of the function. The first column is the time.
stateVarables = [0 10;
1 20;
2 40;
3 50;];
When run the Simulink model, it showed the following error: " Data 'y' is inferred as a variable size matrix, while its properties in the Model Explorer specify its size as inherited or fixed. Please check the 'Variable Size' check box and specify the upper bounds in the size field."
Then I checked "Variable Size" box and put 1 in "Size" textbox. I got another error: "Size computation for 'y' (#831) failed. MATLAB Function Block and Stateflow do not support Variable Sizes for scalar signals."
Then I unchecked "Variable Size" box, and reset -1 in "Size textbox". Then I changed the code as following:
function y = fcn(time,stateVariables)
ii = find(stateVariables(:,1)<=time,1,'last');
if isempty(ii)
ii = 1;
end
y = reshape([ii 0],[2,1]);
end
And it works. But this is not what I want. In this case, I have to add additional column to the output.
My question is: how to get a scalar variable as output of the MATLAB function?
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Naming Conventions finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!