MATLAB: Import constants to simulink

simulink

I want to import some constants values from workspace to simulink.
For example i will write A=10, B=7, C=8 in an m.file then i will run it. So, it will be loaded to my work space.
How can i import them to Simulink ?? From workspace block give me an error message 'Invalid matrix-format …'

Best Answer

Hi,
Use a constant block in Simulink sources and use the variable name (A, B, C, ...) instead of a fixed value. I think with a from workspace block you need to have a Nx2 matrix variable with time in one column and corresponding value in the second column.
Edit:
If your constants are defined in the workspace then you can access them directly from Simulink. If you have A, B, and C, etc defined in the workspace you can use it in any block such as a gain block or a constant or a source block. For example:
% My init function to run before running my simulink simulation
A = 24;
B = 22;
C = .39294;
Then after running this file (you can use Callbacks to run it automatically when you start a simulation as Mischa has described) The variables are available in the workspace for Simulink to access them. Now as an example, I can have a "Constant" (look in "Sources" or "Commonly used Blocks") in my Simulation. I can double-click on it and simply type A or B or C and that block will output that value defined by the constant you enter. You can use the variable in any block that allows a parameter input like this.
Does that help?
Kelly