Cannot Generate Code for Call to Simulink Function From Stateflow Chart - matlab

Consider a Simulink Simulation model including two model-reference blocks, one of them referenced to a model which calls the Add() function, the implementation of the Add() function is in the second model referenced by the second model-reference block.
Now imagine, in the Second model the Add() function is a Simulink Function scoped globally and inside it, there is a MATLAB Function block that calls a Helper() function, note that the Helper() function calls from the MATLAB function. The implementation of the Helper() function is also in the second model as expected.
Now when running the Simulation model the following error is observed:
Cannot Generate Code for Call to Simulink Function 'Helper()' from Stateflow Chart.
Now anyone can help to overcome this problem?
To test the above scenario just download the project from the following link, then open the Simulation.slx and hit run.

Related

Wait for Simulink model execution

I wrote a script file in MATLAB that executes two functions as shown below:
function TempFunction()
fcn1();
fcn2();
end
After fcn1() is executed, I want the program to wait for execution of a model in Simulink, and then execute fcn2().
How can I do that?
One way to do this would be to use a Model Callback - set up a 'StopFcn' to trigger the next thing you want to run.
Alternatively, you could go into a loop polling the 'SimulationStatus' - see this page in the doc. .
you can call simulink models as functions from MATLAB as sim('Model_Name')
Read more about the options in the docs: https://uk.mathworks.com/help/simulink/ug/using-the-sim-command.html
and the sim function: https://uk.mathworks.com/help/simulink/slref/sim.html

Simulink code generation: function stubs from Function Caller blocks and their return values/arguments

In my Simulink model I have several Function Caller blocks like this:
Simple Function Caller block
The function prototype would simply be y = someFunction(). The output argument uses a custom enum type and is given as someEnum(1).
The output signal is defined as one-dimensional throughout.
When generating code from the model, these Function Callers have always yielded a function stub in the expected form of
extern someEnum someFunction(void);.
However, after a lot of changes recently, I've just noticed that code generation now suddenly yields function stubs in the form of
extern void someFunction(someEnum *rty_y);
for some (not all!) Function Caller blocks.
I have compared every parameter about the Function caller blocks and the related output signals that I could find but I can't find any difference between the affected ones and those working as expected in the current version or the same blocks in previous versions. All functions and signals have been renamed, but that's also true for those Function Caller blocks that are not affected.
The Code Generation options are also identical.
I have tried to understand from the help files what might cause the coder to use pointer arguments instead of direct return values for the function stubs but couldn't find anything.
Any hint at what might cause the code generator to use pointers would be greatly appreciated.
Found the problem. Some of the affected blocks had their C/C++ return argument set to "void" in their "Configure C/C++ Function Interface" dialog.
Some of the affected blocks (unfortunately, both of those I had checked before as well) were still set to "y" here and I had to change the setting to "void" and back to "y" before it yielded the desired result.

Specifying Callbacks for Simulink Block Types

I'm trying to figure out how to set a callback for for every instance of a specific MATLAB object type and not a single instance. In my case the object type is either a SimuLink Block, Subsystem, or ModelReference.
After having read
http://se.mathworks.com/help/matlab/creating_plots/callback-definition.html
I tried
function openCallback(src, evt)
disp('here');
end
set(groot, 'defaultBlockOpenFcn', #openCallback);
but it fails as
Error using matlab.ui.Root/set
blockopenfcn is an invalid class name
Is this possible, somehow?
Simulink block callbacks are set using set_param function on a block handle. You would need to set it for a single instance of the block. I do not think it is possible to set it for every instance of a block. If you have a model you can find all the blocks of a type and set their callbacks in a loop.
You can see help for block callbacks at http://www.mathworks.com/help/simulink/ug/block-callbacks.html. You would call set_param as
set_param(gcb, 'OpenFcn', 'disp(''open fcn'')')
where gcb is a function that returns currently selected block in a model.

How to call function inside a main function in MATLAB?

I have a function in MATLAB, say [o1, o2]=MyFunction(i1,i2), and I have a main which is also in a function called main. Is this even make sense?
I did it in MATLAB as follow:
function main
i1=1;
i2=2;
[o1, o2]=MyFunction(i1, i2);
end
function [o1, o2]=MyFunction(i1, i2)
%Code goes here.
end
I cannot run this script anyway. Please any suggestions?
There is no "main" function in MATLAB. You should move its contents to a separate script, like the following:
Script 1:
i1=1;
i2=2;
[o1, o2]=MyFunction(i1, i2);
Script 2 (called "MyFunction.m"):
function [o1, o2]=MyFunction(i1, i2)
%Code goes here.
end
Then run Script 1.
as it has been commented above it works fine... also if all you want the function to do is define simple variables and call another function #ClydeW's answer is a sensible way to do that. For more complicated variables mat-files created with save or matfile and recoverd with load or matfile are available.
In Matlab terminology what you have there is a local function
Local Functions are extra functions defined within a function m-file, appearing after the end of the "main" function. Local functions have a separate workspace i.e. to use variables from the main function they will need to be inputs to the local function
Other alternatives for having "sub-functions" which are stored in the same m-file and used by the "main" function within Matlab are Nested Functions and anonymous functions
Nested functions are similar to a local function but appear within the "main" function definition i.e. before the end. The major difference being that a nested function has acces to the main functions workspace i.e. can use & modify variables from the main function without having them explicity as inputs or outputs
Anonymous functions are quite different in that they need defining with different syntax again within the "main" function, but prior to use (appearing earlier in file than the call to them). The inputs to an anonymous function come from the main function however other values used within an anonymous function use values from the main function at the time the anonymous function was defined.

Simulink difference between load_system and open_system

I have a Simulink model that calls a script in the InitFcn of the Model callbacks. This script initializes a bunch of variables in the base workspace so that they can be used by the Simulink model. When using Classes, I found that using the load_system function will make Matlab crash and the open_system function will work just fine.
Here the Class:
classdef simulinkModel
properties
model = '';
end
methods
function obj = simulinkModel(modelName)
obj.model = modelName;
end
function openModel(obj)
% Make sure any previously open model are closed, and open the model.
if bdIsLoaded(obj.model)
obj.closeModel()
end
%load_system(obj.model) % Matlab crash
open_system(obj.model) % Matlab run the model correctly
end
function closeModel(obj)
close_system(obj.model, 0)
end
function runModel(obj)
sim(obj.model)
end
end
end
And in the command window:
objModel = simulinkModel('test');
objModel.openModel
objModel.runModel
So how comes it crashs with the load_system vs the open_system? Is there something that the open_system function do with the base workspace that the load_system function doesn't do?
EDIT
I decided to try running simple commands outside of the class to see if the problem is elsewhere. So typing in the command window:
load_system('test')
sim('test')
When executing this in the command window, Matlab will also crash. So I'm starting to wonder if the model callbacks are not executed when the load_system function is called.
I can confirm there is indeed very different behaviour between load_system and open_system. I am currently debugging a weird problem under 2015b to be, and I just realized the LoadFcn callback of my simulink models is not called when I use load_system, but is is called properly when I use open_system. I don't know that this is documented anywhere. I find this callback name misleading if it is not called when you "load" the system !!! If I find more info, I will post back on this thread.