How can I send a result to the workspace and take the same result from workspace ??

My simulink model is containing two parts, and each part is formed by differents blocks. In the first part I am doing complicated calculations and sending the final result to the workspace using the "to workspace" block after giving the name A to the variable.
The second part of my model is containing a MATLAB fcn block (the fsolve function)which is taking its guess from the workspace through the block "from workspace" which is having the name of its variable changed to "guess".
My fsolve function needs the variable A because one of the equations that has to be solved is containing the variable A as parameter.
when I type in the command window the value of A and of the guess and after that I run the simulation of the second part, fsolve is working and the results are displayed on the "display block".
but
when I run the simulation of the first part the signs double are displayed in front of A in the workspace but no value for A is given. After that when I run the simulation of the second part of the model(containing fsolve block), fsolve does not work because it can not access the value of A in the workspace.
Please help me solve this problem.

Antworten (4)

Kaustubha Govind
Kaustubha Govind am 13 Feb. 2012
The "To Workspace" block does not write the variable to the workspace immediately, it is only available at the end of the simulation, so the second part of your model cannot access the variable during simulation. Why not connect the signal from the first part directly to the second part?
Btw, also read the second paragraph in my answer on this discussion

6 Kommentare

Hi Kaustubha Govind,
Thank you for your answer.
I can not connect the signal from the first part directly to the second part because in the second part there is only the block "matlab function" which is containing the fsolve function.So the block of "matlab fsolve function" can receive only one signal that is the signal of the "guess" coming from workspace.
Also in order to work, the block of "matlab fsolve function" must access the value of A in the workspace.
Possible solution:
let us suppose that I run simulation of the first part and after the end of the simulation of the first part I run the simulation of the second part. Can the second part access the value of A this way ?????
Yes, you can have each part in a separate model, and run the second part after the first has finished executing.
I have tried that but the value of A is calculated in the first model but still it is not accessible to the second model.
Is the variable "A" created in the base workspace after the first model finishes running? What is the error you get from the second model?
After the simulation of the first model I can see A in the workspace but in front of the value is written : <1x1struct>.
the error messages that I receive are:
Failure in initial user-supplied objective function evaluation. FSOLVE cannot continue.
Undefined function or method 'mpower' for input arguments of type 'struct'
Error in 'fsolvefunction/MATLAB Fcn' while evaluating expression: Undefined function or method 'mpower' for input arguments of type 'struct'.
"A" is getting stored as a structure because the Save Format for your To Workspace block is configured to "Structure" - either change that to "Array", or access A by referencing the corresponding structure field A.signals.values.

Melden Sie sich an, um zu kommentieren.

Trying writing variable A using stateflow chart with ml operator. Chart will update the variable in workspace at every sample time.
{ml.A=Data;}
O--------------------->O

2 Kommentare

Hi TAB,
Please can you explain more. Where do I have to enter the code {ml.A=Data;} ???
I have tried typing this code in the command window but it is not working.
You have to use stateflow chart in the model.
See
>> doc stateflow

Melden Sie sich an, um zu kommentieren.

one thing can be made;if u open the models through a global m-file using sim() command.for the first time when the first model will be exicuted the result will be passed to workspace through the function of the link given below. http://www.mathworks.com/matlabcentral/fileexchange/27106-putvar-uigetvar
Hi kamal,
In your first model, by using To workspace & clock block, save the time in format of array, and the variable which you want to store in workaspace in same format.Then, by using From workspace block, in data parameter, create a matrix with time and variable for ur second model. Maybe it' ll work,try this.

Produkte

Gefragt:

am 13 Feb. 2012

Bearbeitet:

am 13 Okt. 2013

Community Treasure Hunt

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

Start Hunting!

Translated by