Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to transfer the parameters in the base workspace to the embedded matlab function in Simulink model?

2,780 views
Skip to first unread message

Sunset Raisin

unread,
Jan 22, 2009, 5:04:05 AM1/22/09
to
Hi all,

I have a main function, in which I call a simulink model "test.mdl" using the following code:
%-------------------------------------------------
[t,x,y] = sim('test',timespan,options)
%-------------------------------------------------

In the "test.mdl", I build an embedded matlab function block.
%-------------------------------------------------
function dv = dynapf(a, b, c, d, e)
%-------------------------------------------------

The input parameters are produced by my main function, in which 'a' and 'b' are complex vectors, 'c' is a normal vector, 'd' and 'e' are scalars.

I have checked the help and tried two approaches:

1. Use the "From Workspace" block, change the name in "data" field into 'a','b'...respectively, connect these blocks with the corresponding input ports of the embedded matlab function block . When I run the whole program, there is an error message indicating these parameters are not exist.

2. Modify the "Configuration Parameters -->Data Import/Export". In the "Load from workspace area", check "Input" and fill in the box with "[a, b, c, d, e]", but I don't know how to do next.

In summary, my problem is how to transfer these parameters in the matlab base workspace to my embedded matlab function in Simulink model?

I would be very grateful if anyone can give me any suggestions.

David

unread,
Jan 22, 2009, 5:44:02 AM1/22/09
to
"Sunset Raisin" <elino...@hotmail.com> wrote in message <gl9gal$6ms$1...@fred.mathworks.com>...

the 'from workspace' block is the one i would expect to work... but let me guess, you say you have a 'main function' that calls the simulation. is it really a function or just a script? if it is a function then you would have to do assignin or evalin to put the variables into the base workspace. simulink can only access the base workspace, not a function's workspace.

Steven Lord

unread,
Jan 22, 2009, 10:01:37 AM1/22/09
to

"David" <da...@bigcompany.com> wrote in message
news:gl9ili$495$1...@fred.mathworks.com...

... unless, I believe, you were to use the SIMSET function to set the
SrcWorkspace option.

http://www.mathworks.com/access/helpdesk/help/toolbox/simulink/slref/simset.html

http://www.mathworks.com/access/helpdesk/help/toolbox/simulink/slref/sim.html#brjt67g

--
Steve Lord
sl...@mathworks.com


Sunset Raisin

unread,
Jan 22, 2009, 10:36:03 AM1/22/09
to
--------------------------------------------------------------------------------------------------
"David" <da...@bigcompany.com> wrote in message <gl9ili$495$1...@fred.mathworks.com>...

> the 'from workspace' block is the one i would expect to work... but let me guess, you say you have a 'main function' that calls the simulation. is it really a function or just a script? if it is a function then you would have to do assignin or evalin to put the variables into the base workspace. simulink can only access the base workspace, not a function's workspace.
---------------------------------------------------------------------------------------------------
Thanks David.

I use a function not just a script.

I did as you said, adding the script "assignin('base','a',a)" in my main function and therefore copy 'a' to Matlab base workspace, then run the model. This time another error message appears:

"Invalid matrix-format variable specified as workspace input in 'test/From Workspace'. The matrix must have two dimensions and at least two columns. Complex signals of any data type and non-double real signals must be in structure format. The first column must contain time values and the remaining columns the data values."

It indicates me to specify the time (the first column), but 'a' is just a static matrix which is not depent on time. How should I do then?

David

unread,
Jan 22, 2009, 10:55:04 AM1/22/09
to
"Sunset Raisin" <elino...@hotmail.com> wrote in message <gla3p3$23l$1...@fred.mathworks.com>...

sorry, i wasn't quite awake when i wrote that i guess. for constant values use a constant block instead of a 'from workspace' block. 'from workspace' assumes you have a time/value array and wants to read it like a signal generator. in the constant block just change the number to the workspace variable name. the assignin is still the first step anyway, just use a different block for constants.

Sunset Raisin

unread,
Jan 22, 2009, 11:25:05 AM1/22/09
to
----------------------------------------------------------------------------------------------
"Steven Lord" <sl...@mathworks.com> wrote in message <gla1oh$dvv$1...@fred.mathworks.com>...

> ... unless, I believe, you were to use the SIMSET function to set the
> SrcWorkspace option.
>
> http://www.mathworks.com/access/helpdesk/help/toolbox/simulink/slref/simset.html
>
> http://www.mathworks.com/access/helpdesk/help/toolbox/simulink/slref/sim.html#brjt67g
>
> --
> Steve Lord
> sl...@mathworks.com

----------------------------------------------------------------------------------------------
Steve, thanks very much for your information.

I have concluded the following script in my main function:

options = simset('SrcWorkspace', 'current');

I'm still debugging, hope it works.

Sunset Raisin

unread,
Jan 22, 2009, 11:34:01 AM1/22/09
to
---------------------------------------------------------------------------------
"David" <da...@bigcompany.com> wrote in message <gla4so$g7f$1...@fred.mathworks.com>...

> sorry, i wasn't quite awake when i wrote that i guess. for constant values use a constant block instead of a 'from workspace' block. 'from workspace' assumes you have a time/value array and wants to read it like a signal generator. in the constant block just change the number to the workspace variable name. the assignin is still the first step anyway, just use a different block for constants.

---------------------------------------------------------------------------------
I use the constant block instead of the 'From Workspace' block and it works! Thanks very much David!

0 new messages