Error in using variable from MATLAB workspace to Simulink?

75 visualizaciones (últimos 30 días)
In my case I have one model file of Simulink. So when for the first time I run the model and I send one signal which I named as "Fs" in the "To Workspace" block. Then I use this "Fs" from the matlab workspace to calculate 'Rate of change of this Frequency signal' using matlab script. I have named the rate of change of frequency as variable "avg_rocof". Now I wanted to use this "avg_rocof" variable in my Simulink model using "FromWorkspace" block. But when I run the model I get the following above error
" Invalid matrix variable specified as workspace input in 'IEEE_14_Bus_System_SM_FFR/From Workspace'. The matrix data must be a real, double, have two dimensions, and at least two columns. The first column must contain time values and the remaining columns the data values. Matrix values cannot be Inf or NaN.
Anyone please assist in this problem?

Respuesta aceptada

Walter Roberson
Walter Roberson el 5 de En. de 2023
When you use "From Workspace", the variable must have at least two columns. The first column will be treated as time.
If you are trying to import a value that is not logically a "signal" that varies in time, then there are a small number of possibilities:
  • it is a signal, just a constant signal; in that case use 0 as the first column (time); and configure From Workspace to "hold" the signal; OR
  • it is something you want to treat as a global variable or datastore variable; OR
  • it is something you want to use as a parameter (for example gain for a block)
If you are wanting to do global variables or parameters, then see https://www.mathworks.com/matlabcentral/answers/1653710-global-variable-in-simulink#comment_2009495 where I show how to construct those from the MATLAB level. There are some tricks involved
  14 comentarios
Walter Roberson
Walter Roberson el 12 de En. de 2023
My recollection is that at some point in the past, while testing/fixing code people had posted, that Simulink did read variables from the calling function workspace. It might well have been before R2017b.
Much more recently, I have observed Simulink prompting to create a variable even though there was no DataStoreRead . I encountered the behaviour when I was building the code I linked to above -- one of my goals for that code was to create the objects at the MATLAB level in such a way that Simulink would not pop up such prompts.
Paul
Paul el 12 de En. de 2023
I can't say that I've used Simulink in every release, but I'm pretty sure that running a model using the sim command (which is what I think you're talking about here, correct me if I'm wrong) was never supposed to use variables from the calling workspace by default. Hence the need for the 'SrcWorkspace' Name/Value parameter, which defaults to 'base' and always has as far as I can remember.
I do not doubt that Simulink might prompt to create a variable (never experienced that myself). However, if that happened in the context of developing the code referenced in this link, then that is a different issue. That Answer thread referenced this doc page Share Data Globally. However, that doc page is talking about using "global data with a MATLAB Function block," which is a different concept than using variables to define block parameters, which is what we've been talking about in this Answer thread, whether for the Constant block or the From Workspace block.

Iniciar sesión para comentar.

Más respuestas (1)

Fangjun Jiang
Fangjun Jiang el 5 de En. de 2023
Editada: Walter Roberson el 5 de En. de 2023
The error message has explained the problem. Maybe you are not familar with the usage. Double click the "From Workspace" block, click Help to open the document. There should be some example models. You can look into the example models to figure out the proper format for importing your "avg_rocof" data.
Basically, if avg_rocof=rand(10,1), a column vector, then you need to construct a column time vector, e.g. t=(0:0.1:0.9)'
then the data for the "From Workspace" block should be [t;avg_rocof]
  3 comentarios
Fangjun Jiang
Fangjun Jiang el 5 de En. de 2023
avg_rocof is a scalar value, not a vector?? 'Rate of change of this Frequency signal' sounds like a time-varying signal, not a single value.
But, if you are just take the average value, like the name indicates, then you can use a Constant block, not the "From Workspace" block. Specify the Constant value as avg_rocof.
Taimoor Khan Mehmand
Taimoor Khan Mehmand el 7 de En. de 2023
@Fangjun Jiang Thank you for your valuable comments and help.

Iniciar sesión para comentar.

Categorías

Más información sobre User-Defined Functions en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by