0

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.

Simulink Blocks

PreLoadFcn

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?

2 Answers 2

0

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.