From: Ryan Osborn on
Hi,

Is it possible to use a workspace variable inside an embedded matlab function in simulink? I have tried putting a global statement in the code to see if this would work, but it just gave an error.

Thanks,

Ryan
From: Zaki Shirwani on
You'll have to pass it as a "paramater" to the Embedded MATLAB function.
For example, assuming you have a MATLAB workspace variable called
"workspace_var"; you'll first add it to the function signature like any
regular input, then go to "Ports and Data Manager" and set its scope to
"Parameter".

%%%
function y = fcn(u, workspace_var)
y = workspace_var;
%%%

Note that no new input port would be added to the Embedded MATLAB Function
block for this parameter, and it will pick up whatever value you assigned to
"workspace_var" in MATLAB workspace.

HTH.

P.S.: Globals in Embedded MATLAB Function blocks are used in conjunction
with Simulink Data Store Memory blocks. You can read up more about this
here:

http://www.mathworks.com/access/helpdesk/help/toolbox/simulink/ug/bsds2rv.html


"Ryan Osborn" <ryan.osborn88(a)gmail.com> wrote in message
news:hthcl3$2vi$1(a)fred.mathworks.com...
> Hi,
>
> Is it possible to use a workspace variable inside an embedded matlab
> function in simulink? I have tried putting a global statement in the code
> to see if this would work, but it just gave an error.
>
> Thanks,
>
> Ryan


From: Ryan Osborn on
"Zaki Shirwani" <shirwani(a)mathworks.com> wrote in message <hthecn$rsj$1(a)fred.mathworks.com>...
> You'll have to pass it as a "paramater" to the Embedded MATLAB function.
> For example, assuming you have a MATLAB workspace variable called
> "workspace_var"; you'll first add it to the function signature like any
> regular input, then go to "Ports and Data Manager" and set its scope to
> "Parameter".
>
> %%%
> function y = fcn(u, workspace_var)
> y = workspace_var;
> %%%
>
> Note that no new input port would be added to the Embedded MATLAB Function
> block for this parameter, and it will pick up whatever value you assigned to
> "workspace_var" in MATLAB workspace.
>
> HTH.
>
> P.S.: Globals in Embedded MATLAB Function blocks are used in conjunction
> with Simulink Data Store Memory blocks. You can read up more about this
> here:
>
> http://www.mathworks.com/access/helpdesk/help/toolbox/simulink/ug/bsds2rv.html

Hmm, ok, but if its passed as a parameter, does that mean I will not be able to change its value in the workspace?

Thanks,

Ryan
From: Zaki Shirwani on
You will if it's a tunable parameter. There's a "Tunable" checkbox that
shows up in the "Ports and Data Manager" when you change the data scope to
"Parameter". If it's checked (default) you can change the value of the
parameter in MATLAB workspace and it will be picked up without causing code
regeneration. However, if you change the type/size/complexity of the
variable that will cause code to regenerate.

"Ryan Osborn" <ryan.osborn88(a)gmail.com> wrote in message
news:htheul$7ar$1(a)fred.mathworks.com...
> "Zaki Shirwani" <shirwani(a)mathworks.com> wrote in message
> <hthecn$rsj$1(a)fred.mathworks.com>...
>> You'll have to pass it as a "paramater" to the Embedded MATLAB function.
>> For example, assuming you have a MATLAB workspace variable called
>> "workspace_var"; you'll first add it to the function signature like any
>> regular input, then go to "Ports and Data Manager" and set its scope to
>> "Parameter".
>>
>> %%%
>> function y = fcn(u, workspace_var)
>> y = workspace_var;
>> %%%
>>
>> Note that no new input port would be added to the Embedded MATLAB
>> Function block for this parameter, and it will pick up whatever value you
>> assigned to "workspace_var" in MATLAB workspace.
>>
>> HTH.
>>
>> P.S.: Globals in Embedded MATLAB Function blocks are used in
>> conjunction with Simulink Data Store Memory blocks. You can read up more
>> about this here:
>>
>> http://www.mathworks.com/access/helpdesk/help/toolbox/simulink/ug/bsds2rv.html
>
> Hmm, ok, but if its passed as a parameter, does that mean I will not be
> able to change its value in the workspace?
>
> Thanks,
>
> Ryan


From: Ryan Osborn on
"Zaki Shirwani" <shirwani(a)mathworks.com> wrote in message <htj8rt$iiv$1(a)fred.mathworks.com>...
> You will if it's a tunable parameter. There's a "Tunable" checkbox that
> shows up in the "Ports and Data Manager" when you change the data scope to
> "Parameter". If it's checked (default) you can change the value of the
> parameter in MATLAB workspace and it will be picked up without causing code
> regeneration. However, if you change the type/size/complexity of the
> variable that will cause code to regenerate.

I did a simple test, I set x=2 in the workspace, then passed x to my embedded matlab function as a tunable parameter. In the code I then did x=x*2, but when I look in the workspace it has not changed?

Ryan