Mathworks Simulink: S-Function Builder - simulink

I have a library with numerous S-function Builder blocks.
If I want to regenerate .mex files (say, on a different platform),
the standard method would be to:
Go to every S-function builder block
Open its S-Function Builder Wizard
Click Build
Is there a method to automate this process, such that
every S-Function Builder block within a library (including all subsystems),
is rebuilt?

That is possible:
blkNames = find_system(bdroot,'lookundermasks','all','BlockType','S-Function');
for blkIdx = 1:numel(blkNames)
blkHandle = get_param(blkNames{blkIdx}, 'Handle');
appdata = sfunctionwizard(blkHandle,'GetApplicationData');
appdata = sfunctionwizard(blkHandle,'Build',appdata);
end
I don't have any reference or documentation, just read this question shortly after reading your question and verified the code is actually working.

Related

Matlab simulink c code generation

I would like to import an existing C code (or any other text) into my generated code by Matlab simulink.I have some tasks that made in C,but in the future i want to develop in matlab.I work in simulink,and I can compile the models,but I want to use some special function what I previously wrote in C (because of pointer etc.).
The problem is that I don't know how to put in these texts into the model,and after the code generation these texts stay in the original format,and placed in the expected line.
And what I would like:
You can achieve this using the S-Function Builder. It allows one to create C code blocks, which get compiled with the model run. If using Code Generator, it gets inserted into generated code.
I generally use it to call functions from my external code or libraries, as in some Raspberry Pi Driver blocks I created.
It generates .c, .h and .mex files for each block and is quite clunky but does work!
BTW: If it's just to use an external pointer, you can happily use ImportedPointer/ExportedPointer for this. I find this handy for variables between generated code and container.

Matlab/Simulink - Create S-function from DSpace Code

I know that it is possible to create S-function from C code that I provide.
But is it possible to create one S-function from C Code, which is generated from a Simulink Model for dSPACE ECUs, with low effort. The reason is, I am trying to test the dspace code with matlab/simulink.
Furthermore I'm aksing because the generated code from this simulink model consists multiple .c and .h files and I don't know how to integrate these files in one s-function block.
There are several methods of doing this. I suggest using the Legacy Code Tool as its structure helps to guide you through the process. You could also explore calling the function(s) from Stateflow or a Matlab Function block depending on the application.
With respect to multiple c/h files. I assume they will all need to be visible to Matlab through pointing to the source paths, but you should be able to identify the 'entry' function that you're interested in and utilize that in your model.

Is there something like a "pre-build" callback function?

I have a Simulink model, the purpose of which is automated code generation.
My model uses S-functions (developed by another party), which has hard-coded assumptions about the path. For instance, several external data files are needed, which are referenced in the S-function via a relative path like ..\Bin\data\datafile.bin. This makes it necessary to set MATLAB's current working directory to a specific path before the model can be run.
I can automatically check and set the correct path via model callback functions. However, all model callback functions only seem to be related to the simulation process, not the build process. That means that I can run the model irrespective of what directory I'm in, but when I try to build the model, it always fails unless I manually navigate MATLAB back to the correct directory.
Needless to say, that's quite annoying. So I was wondering if there is something like a "preBuildFcn" callback fnuction, a function that is run before starting the build process? Any other solution (that does not involve modifying the S-function) is also very welcome.
There are plenty of hooks into the build process of Simulink / Embedded Coder ('entry', 'before_tlc', 'after_tlc', 'before_make', 'after_make', 'exit', and 'error'). I assume you want an 'entry' hook.
All you need to do is write an M-function with the name your_system_target_file name_make_rtw_hook, as explained in the documentation Customize Build Process with STF_make_rtw_hook File.
In case you can't open the online documentation (login required), here is the path to the HTML in your MATLAB installation: MATLAB root\help\rtw\ug\customizing-the-target-build-process-with-the-stf-make-rtw-hook-file.html
I am not sure whether building simulink models is sufficiently similar to building regular MATLAB programs, but here is what I used in the past:
Set up the project manually
Build the project programmatically
The program that is used to build the project should be able to set the path or do other custom things.

Using model callback with Simulink Coder

I am using Matlab 2012a and the Simulink Coder (aka Real-Time Workshop). I want to compile the model using Simulink Coder but preserve the functionality of model callbacks.
Consider the following simple example. I have a Simulink model, callBackTest, which reads in a constant and outputs to a since. input1 is defined in myValues.m and loaded into the model workspace using the PreLoadFcn model callback. The PreLoadFcn callback is executed when the model is first opened. By using the PreLoadFcn callback, input1 will automatically be defined every time the model is opened.
Suppose myValues.m is originally coded as input1=1. When you run the simulation, yout will be an array of 1s. Also if I compile the model using the Simulink coder, the output will also be an array of 1s. However if I modify myValues.m so that input1 = 2 and do not recompile, the realtime output is still 1. This is wrong, so how can I read variables from a file into the model workspace with a compiled model?
You cannot generate code for model callbacks. If you do not want to regenerate code every time you change your input you can try using "From File" block which can read data from a .mat file. When you want to change your data you can then run your MATLAB code and save the output data into the same .mat file. There are some restrictions on what kind of data is supported for code generation from this block. Check out the doc for that block for details.
If your data is not too big you can also edit the generated source to modify the data. Data from Constant block is usually in-lined in the generated source code. After editing you can compile the generated code to produce new binary.
Another approach is to write your custom C S-Function where you can read from your own data sources. You need to write a TLC file to support code generation for this S-Function.
You need to recompile your model if these does not work for you. Documentation at http://www.mathworks.com/help/simulink/ug/importing-signal-data-in-simulink.html lists different ways of importing signal data into Simulink.
This does not answer your question about Model callbacks, but it might be helpful anyway.
If the "Inline Parameters" option is checked in:
Preferences -> Optimization -> Signals and Parameters
there is no way to change values in an already compiled model because they are hardcoded. Once you have this option turned off and you have recompiled, you could for example connect with external mode and run your myValues.m script and the values will be updated (unless you have marked them as non-tuneable).

Load code for a MATLAB function block at Simulink runtime

I would like to have some file, myfunc.m, in my MATLAB path and somehow load its contents into a MATLAB function block automatically just before the simulation starts. This way, I can use an external editor to write these embedded function, version control them separately as independent files, etc.
Is there a way to achieve this programmatically?
My initial attempt was to try and access the contents of the function block using something like get_param, but I can't seem to gain read/write access to the code itself.
If the target MATLAB Function block doesn't already exist then you can add it as follows (see this SO post):
load_system('eml_lib');
libname = sprintf('eml_lib/MATLAB Function');
add_block(libname,'myModel/myBlockName');
You can then modify the block's code using the Stateflow API:
sf = sfroot();
block = sf.find('Path','myModel/myBlockName','-isa','Stateflow.EMChart');
block.Script = 'Your code goes here';
See also this post on MATLAB Answers.
First, you will need to add the folder containing the m-file to the default path. To do this:
(In the Command window)
Go to File -> Set Path -> Add Folder (choose the folder containing the m-file)
Now, you should use the InitFcn callback in the model properties to call your function. To do this, open the model:
(In the Model window)
Go to File -> Model Properties -> Callbacks -> InitFcn
In the edit box provided for the InitFcn, write the command to call your function i.e. myfunc();
You will have to modify this command as per your function and requirements.
Once done, apply the changes to the Model Properties window and simulate the model.
I am thinking that model callbacks may be a way to do what you want, though I have not used this technique myself.