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

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?

Related

How do I input variables into a Simulink model through MATLAB script (SimDriveline)

For my coursework project in MATLAB, I have decided to build a drive-line model within Simulink, using the SimDriveline toolbox. The idea is to get the user to input values for the various parameters that are associated with each part of the model, such as the engine or the transmission. I would like to able to write this in a MATLAB script, but I'm not sure how to assign the values that are input to the Simulink model. For instance, the stock sdl_vehicle example that comes with SimDriveline. I am aware of the sim() command, but I am still confused on how to use it properly.
Also at the end of the simulation, the program is supposed to display the graphs that are collected in the scope window. I know that in the window itself that the scope can be printed to a figure, but is it possible to print that scope to a figure through MATLAB script?
This is the first time I have ever used a program like MATLAB. I would appreciate any help I could get, many thanks in advance!
There is a simulink block called simin:
http://de.mathworks.com/help/simulink/slref/fromworkspace.html?searchHighlight=simin
I used it some days back and it worked quite well. You can use the block in your model and define some signals/varibles as input.
After that you may write a Matlab-Script with an input function to set all the previous defined input values.

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.

Simulink memory error in "frame processing mode"

Error:
Cannot propagate frame-based signal through input 'MATLAB Function5' because this input
expects a sample-based signal. The frame-based signal originates from 'mzmzmzcpy/MATLAB
Function5/ SFunction '. Consider inserting an Unbuffer in the signal path.
I am using frame based processing, I save a whole frame in memory and want to read this frame
at the time I need it at the input of same block but every time I get this error, I have tried
both "memory" and "data store memory" blocks, can anybody help me to know how can I change
sampling mode of input as I have tried using "converter" block to change output of "data read
memory" block to frame output even then i found the same error(Cannot propagate frame-based
signal through input 'MATLAB Function5' because this input expects a sample-based signal), then
i tried using buffer but then i found the error given at the beginning of this note.
Have you defined the output(s) of your MATLAB Function block "MATLAB Function5" to be frame-based rather than sample-based? See MATLAB Function Block Editor in the documentation for more details.
EDIT:
If I understand correctly, from your picture, it looks like the problem is with the signal coming out of the memory block. I think memory blocks do not support frame-based signals (or at least, from what I can see in the documentation). As suggested in the comments, try inserting a Signal Specification block before feeding the signal to the MATLAB Function block to enforce a frame-based signal. Failing that, I am not sure, you may want to contact MathWorks for help.

control simulink from M-file

I am trying to control a simulink from a M-file.
What I want to do in the M-file is give the simulink model some input, run the simulink model, change one input value at 0.6 seconds, continue running the simulink model with the new input.
I already know that by using set_param, I can start, pause and continue the simulink, but the problem is I don't know how to pause the simulink model at a certain time(0.6s), is it possible to get the current time from simulink model and read it in the M-file?
Another way I already know is using sim to run simulink model from 0 to 0.6s, and use SimState to save the information at 0.6s, then load these information to resume the simulation. I am trying to change the input before the simulation resumed, but it seems that the model will load the input values from the information it saved, it won't take the new input value.
I stuck in this problem for a very long time, could someone help me with this please?
Thank you very much.
You can get the current time of a running simulation with:
get_param('simulink_model_name', 'SimulationTime');
So for instance by checking this value from your M-file during simulation by using
timer(...)
you can detect when the simulation is at 0.6 seconds.
I used a combination of simulink and m-script to achieve a similar goal.
In your model, add one 'assert' block. Double click it, and uncheck 'Stop Simulation when assertion fails'. In the 'Simulation Callback when assertion fails' field, add three commands:
set_param(bdroot,'SimulationCommand','pause');
run('myscript.m'); %insert the script name
set_param(bdroot,'SimulationCommand','continue');
Now connect the inport of this block to a 'not equal to' relational operator. Connect the first inport of the relational operator to a clock (pls set the decimation for analog clock or the sample time [usually -1 for inherited] for the digital clock).
The second inport is connected to constant block with a value of 0.6
On simulating the model, the simulation will pause at 0.6 sec, execute the m-file to change the input parameter (considering that it's tunable) and then continue with the simulation.
The assertion block is called when its input signal becomes 0. At 0.6 sec, the output of the relational operator will be 0.
Let me know if it worked.
This is not currently possible from an M-file. If you want to dynamically change the input at a given time externally, it will require an S-Function. Even this solution is difficult and wrought with flakey-ness since the Mathworks does not want to support this functionality in that it defeats one of the features of another toolbox they sell. In time, I believe they will grant this privledge, but it does not exist today. Also, why not use a dynamic input block to change the input value, like a map, signal builder, etc. ?

How can the output of a Simulink block be fed back as an input?

I have 2 embedded Matlab functions which I am using to create a Simulink model. Both functions use the output of the second function as their input. I am getting an error at the moment indicating that this is an invalid loop.
Does anyone know how to implement this type of behaviour?
You've created an algebraic loop, which means that to compute the inputs of the Embedded MATLAB block are directly dependent on the outputs of the block. This is not allowed when the loop is a "self-loop", i.e. there is only one block in the loop.
One way to fix this is to put Unit Delay block(s) somewhere on the signal feeding back into the Embedded MATLAB block. See the documentation on algebraic loops for more information.