Refresh Simulink S-function block - simulink

I have an S-function block in Simulink, which does not update when I change the number of input ports in its .C source file. I recompile the code and then I would like to modify the block's mask and add one more port label but I get the error saying that the number of input ports has been exceeded. How do I force Simulink to reload that S-function block? I already tried Diagram->Refresh blocks with no success.

I just found the error... Simulink calls mdlInitializeSizes to figure out the block's number of inputs and outputs. However, I also have some parameters that I pass to the S-function and those were not initialized and hence mdlInitializeSizes could not run. Fixed.

Related

Can I set the parameters in a matlab function in simulink to be tunable?

I want to tune some parameters of a matlab function block in simulink with Dashboard Blocks like knobs. But when i click on the matlab function with the parameter I want to tune, it says that this block has no tunable parameters. How can I change this?
To my knowledge there is no way to get a tunable parameter in such a block. To get the functionality, I recommend make an input port out of the parameter you want to tune. Then connect it to a constant block and tune the constant instead.

Simulink: individual signal output from bus input (based on signal name)

I have a complex Simulink model and want to generate in a subsystem one simple error message signal out of a lot of incoming error observing signals.
These incoming signals are organised in a bus and are always 0, exept when an error occurs: then there is an edge to value.
The error message should contain an individual bit code which is defined for every error name and error value in an excel list.
How can I assign this error code to a specific error signal with rising edge? I need something like a lookup function:
for (every signal):
if signal has edge to value,
look in list to signal name
and output its error code.
My idea was to use a MATLAB function block with a bus input and a one-dimensional output. A .mat file generated before Simulation start contains a struct with the list inputs from the excel file and should be used as a parameter in the MATLAB function.
This solution seems a bit difficult though, since dynamic field referencing and interating through a structure is not supported for code generation. I cannot even get the signal name from the struct because the fieldnames function is not supported, too.
I am now wondering if there is another approach or the MATLAB function is still doable for my problem. Can I generate the code for the MATLAB function block in a PreLoadfcn? Does a S-function make sense here? Or is there even a Simulink based solution which does the Signal conversion?

How can I change a Block parameter in Simulink with Serial Port?

I am loading my Simulink Model to a microprocessor. After that I want to change a value of a Block parameter.
In the picture below the data comes to Matlab Function block is coming from Serial Port. Now, appearently th incoming data is assigned to input u of Matlab Function block. In that Matlab Function block I want to write a code that changes , for example, coefficient of s in the Transfer Fcn block. Or it can change any other parameter you can give in code hypothetically.
If anybody can give me any solution that would be ver much appreaciated!. Thank you in advance.
Substitute a custom-made subsystem for the transfer function whose parameters have to change, and create the subsystem from basic blocks so that coefficients can be read from signal sources. You might be interested in this
http://blogs.mathworks.com/seth/2011/03/08/how-do-i-change-a-block-parameter-based-on-the-output-of-another-block/
Even if you don't want to change parameters at every time step, the restriction
"[...] calls to set_param also preclude the model from ever being compiled
into a real-time application."
seems relevant to your case.
Anyway, as far as I know the post linked above still applies in 2014.
Hope that helps.

Initialization of a Dymola FMU in Simulink

I have encountered an issue while trying to simulate a model using an fmu file in Simulink:
I am using the Modelon FMI toolbox for a Dymola/Simulink interface to import a Model-Exchange type fmu file (version 1.0) into the Simulink model. I have a Matlab m-file that programmatically simulates the model sequentially, saving the final internal states of the model for initialization in the next simulation. The first iteration of the simulation runs without errors, but on the second iteration, Matlab returns the following errors at the call for the simulation:
module = partial_run_sim2/BasicModels.System_model_final_Sim, log
level = ERROR: [][FMU status:Error] fmiInitialize: dsblock_ failed,
QiErr = 1
module = partial_run_sim2/BasicModels.System_model_final_Sim, log level =
ERROR: [][FMU status:Error] Possible errors (non-exhaustive): 1. The
license file was not found. Use the environment variable
"DYMOLA_RUNTIME_LICENSE" to specify your Dymola license file. 2. The
model references external data that is not present on the target
machine, at least not with the same location. Error reported by
S-function 'sfun_fmu_me_1_0' in 'partial_run_sim2/BasicModels.System_model_final_Sim': fmiInitialize
returned with an error in initialize_fmu_model_first_time. See the
Command Window for more information printed by the FMU model.
I know that my licenses for Dymola and the FMI toolbox are up-to-date.
The simulation call in the m-file is:
simOut_itr = sim(model, model_cs);
Where “model” is the Simulink .slx model file and “model_cs” is a Simulink Configuration Setting variable. I have set the Configuration such that the model loads initial states from the workspace, and I have saved the previous iterations final states to that workspace variable. This action occurs for the first iteration as well, though the initial states are the same as the default initial states in the fmu file. I suspect that the first iteration runs without issue because the initialization is the same as the default value, but the subsequent iteration has a different initialization value, which does not agree with the fmu block.
I am aware of the ability to simulate the fmu file in Matlab using the fmi functions, but these methods do not allow for as much transparency into the system states, which hinders the sequential simulation. I am open to any methods that allow this kind of simulation, however.
Any suggestions with regard to initializing the system states in fmu files in Matlab/Simulink are much appreciated. Thank you.
I think I have encountered this error before, but not with the FMI Toolbox.
I had issues initializing a Dymola generated FMU more than once in a for-loop very similar to what you described above with the output of one initialization as the input for the next.
Give some random (but reasonably good values) and initialize the model just once such that the initialization is not the same as the default value.
If this works, then in all likelihood, the problem might be in multiple-initializations. Perhaps, you can unload the model at the end of each initialization and then load it again at the beginning of the each loop?
If nothing works, perhaps u can use JModelica.org (open source platform from Modelon) to get the output of these initializations into a mat-file and then use it in MATLAB for subsequent work. I know this is not ideal, but might save you some time while you wait for some answers here.

Runtime drawing subsystem icon based on output results

Does someone know a way how to update/draw subsystem's input or output results in runtime? In this sense, one could do a Scope block, which updates itself during the simulation, so one could see the results already by looking at the block.
Of course, my intention is not making a scope block but make some custom drawings based on results inside the subsystem in runtime.
This would mean either to:
be able to access a variable with results in plot command of masked subsystem and making sure that Simulink calls refresh everytime the variable changes
change MaskDisplay from outside, for example by inputting absolute values in plot command and signaling to Simulink that it needs to refresh that Subsystem's drawing
One way of to use a MATLAB Function block and declare plot, for example, as an extrinsic:
coder.extrinsic('plot');
You can then use plot in the MATLAB Function. You can also do this for a custom MATLAB file:
coder.extrinsic('my_custom_draw_function');
Somewhat more complicated can be to use m-file s-function.