How to read Simulink inputs from Matlab ?

Hi all
I am trying to model some mathematical equation in Simulink where some of the inputs are constant number, some are varying with time and some are words like choices (yes or no)
i want to put all my variables in one file (for example .m file to be easily edited) then read it from Simulink.
i tried to write .m file then run it and print variables on the command window or the workspace then put a 'From workspace' block with the variable name in Simulink but i found an error because of 'Invalid matrix-format variable specified as workspace input'
any suggestions ??
Thanks

 Réponse acceptée

Jacob Halbrooks
Jacob Halbrooks le 18 Mar 2014
Modifié(e) : Jacob Halbrooks le 18 Mar 2014
This error suggests that you are not using a data format that the block supports. See the Specifying the Workspace Data section of the block's reference page for more information. In addition, the block's dialog provides guidance on how to format matrix or structure data:
For matrix formats, each row of the matrix has a time stamp in the first column and a vector containing the corresponding data sample in the subsequent column(s).
For structure format, use the following kind of structure:
var.time=[TimeValues]
var.signals.values=[DataValues]
var.signals.dimensions=[DimValues]
If you do not actually need time-varying data, then consider using a Constant block instead, where you can enter your data's workspace variable name in the "Constant value" box.

8 commentaires

ameen
ameen le 19 Mar 2014
Thank you for your kind reply but i have over 20 inputs to my model some of them are constants some of them are time varying variables and some are words like yes or no. So, i need to put all my input in one file to make it easy for the model users to edit these inputs
Jacob Halbrooks
Jacob Halbrooks le 19 Mar 2014
It should be fine to load all of your data from a single file. However, in your Simulink model, you may need multiple source blocks to get the correct data read into your model.
ameen
ameen le 19 Mar 2014
my problem now with the words i want to use as a variable values. like yes or no.
how can i use words as a variable value ??
Jacob Halbrooks
Jacob Halbrooks le 19 Mar 2014
Can you describe how those variables containing words will be used in your model? What blocks do you want to use them, and how?
ameen
ameen le 19 Mar 2014
i am a ship engineer and i am trying to simulate ship's performance so for example i have a variable named 'ship_draft' with two possible answers which are: 'design_draft' or 'ballast_draft'.
Then, i made an 'if' statement.
If the 'ship_draft' == 'design_draft', a=b+c
If the 'ship_draft' == 'ballast_draft', a=b-c
That's what i am trying to do
Jacob Halbrooks
Jacob Halbrooks le 19 Mar 2014
There may be many ways to do this in Simulink, but here's one approach (assuming b and c are workspace variables):
  • Create a Constant block with value "b+c"
  • Create another Constant block with value "b-c"
  • Create another Constant block with value "double(strcmp(shift_draft,'design_draft'))". This will produce a value that is either 0 or 1, which you can use to switch between the other Constant blocks
  • Create a Switch block with the output of "b+c" as the first input and the output of "b-c" as the third input. The second input is the threshold, which the other Constant block feeds. On the block's dialog, set Threshold to 0 and Criteria for passing first input to "u2 > Threshold"
ameen
ameen le 20 Mar 2014
Thank you very much for your help.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by