Thread Subject: Embedded Matlab and Parameters

Subject: Embedded Matlab and Parameters

From: elizabeth weitzel

Date: 1 Jul, 2008 19:10:18

Message: 1 of 9

Hello,

I am at a loss and could use a bit of advice from those
more experienced than I.

I have a program that needs to be run in a simulink model,
and this program is composed of many mfile scripts. I
understand I can use an embedded matlab function and call
these mfiles extrinsically.. but the problem I have is
with the initialization parameters.

I have over 30 initialization parameters that are accessed
by the subfunctions, and the only way I can see of feeding
that information to the simulink model is as inputs to the
embedded matlab block. That seems really messy.
I was wondering if there is a way to get these mfiles to
see the parameters in the base workspace.
I could run an initialization script to populate the
workspace before running the simulink model... but I need
this program to see those parameters.

Got any ideas?
Thank you in advance!
-Elizabeth

Subject: Embedded Matlab and Parameters

From: Michael Hosea

Date: 1 Jul, 2008 20:26:32

Message: 2 of 9

Have you tried running your initialization script and then running a
Simulink model in which Embedded MATLAB blocks call your other scripts
extrinsically? What (if anything) goes wrong? I think, whether or not one
admires the system design, it should work as you describe.
--
Mike

"elizabeth weitzel" <elizabeth.weitzel@baesystems.com> wrote in message
news:g4dveq$qg4$1@fred.mathworks.com...
> Hello,
>
> I am at a loss and could use a bit of advice from those
> more experienced than I.
>
> I have a program that needs to be run in a simulink model,
> and this program is composed of many mfile scripts. I
> understand I can use an embedded matlab function and call
> these mfiles extrinsically.. but the problem I have is
> with the initialization parameters.
>
> I have over 30 initialization parameters that are accessed
> by the subfunctions, and the only way I can see of feeding
> that information to the simulink model is as inputs to the
> embedded matlab block. That seems really messy.
> I was wondering if there is a way to get these mfiles to
> see the parameters in the base workspace.
> I could run an initialization script to populate the
> workspace before running the simulink model... but I need
> this program to see those parameters.
>
> Got any ideas?
> Thank you in advance!
> -Elizabeth


Subject: Embedded Matlab and Parameters

From: elizabeth weitzel

Date: 1 Jul, 2008 20:59:02

Message: 3 of 9

Yes, I run an init script that sets the parameters to the
base workspace, but when I compile and run, it tells me
that the parameters are not defined and gives me links to
the extrinsic functions.


"Michael Hosea" <mhosea@mathworks.com> wrote in message
<g4e3to$5fa$1@fred.mathworks.com>...
> Have you tried running your initialization script and
then running a
> Simulink model in which Embedded MATLAB blocks call your
other scripts
> extrinsically? What (if anything) goes wrong? I think,
whether or not one
> admires the system design, it should work as you
describe.
> --
> Mike
>
> "elizabeth weitzel" <elizabeth.weitzel@baesystems.com>
wrote in message
> news:g4dveq$qg4$1@fred.mathworks.com...
> > Hello,
> >
> > I am at a loss and could use a bit of advice from those
> > more experienced than I.
> >
> > I have a program that needs to be run in a simulink
model,
> > and this program is composed of many mfile scripts. I
> > understand I can use an embedded matlab function and
call
> > these mfiles extrinsically.. but the problem I have is
> > with the initialization parameters.
> >
> > I have over 30 initialization parameters that are
accessed
> > by the subfunctions, and the only way I can see of
feeding
> > that information to the simulink model is as inputs to
the
> > embedded matlab block. That seems really messy.
> > I was wondering if there is a way to get these mfiles
to
> > see the parameters in the base workspace.
> > I could run an initialization script to populate the
> > workspace before running the simulink model... but I
need
> > this program to see those parameters.
> >
> > Got any ideas?
> > Thank you in advance!
> > -Elizabeth
>
>

Subject: Embedded Matlab and Parameters

From: Michael Hosea

Date: 1 Jul, 2008 21:41:32

Message: 4 of 9

It worked for me, but I'll have to try it in the release that you're
actually using. Which release is that?
--
Mike

"elizabeth weitzel" <elizabeth.weitzel@baesystems.com> wrote in message
news:g4e5qm$9jp$1@fred.mathworks.com...
> Yes, I run an init script that sets the parameters to the
> base workspace, but when I compile and run, it tells me
> that the parameters are not defined and gives me links to
> the extrinsic functions.
>
>
> "Michael Hosea" <mhosea@mathworks.com> wrote in message
> <g4e3to$5fa$1@fred.mathworks.com>...
>> Have you tried running your initialization script and
> then running a
>> Simulink model in which Embedded MATLAB blocks call your
> other scripts
>> extrinsically? What (if anything) goes wrong? I think,
> whether or not one
>> admires the system design, it should work as you
> describe.
>> --
>> Mike
>>
>> "elizabeth weitzel" <elizabeth.weitzel@baesystems.com>
> wrote in message
>> news:g4dveq$qg4$1@fred.mathworks.com...
>> > Hello,
>> >
>> > I am at a loss and could use a bit of advice from those
>> > more experienced than I.
>> >
>> > I have a program that needs to be run in a simulink
> model,
>> > and this program is composed of many mfile scripts. I
>> > understand I can use an embedded matlab function and
> call
>> > these mfiles extrinsically.. but the problem I have is
>> > with the initialization parameters.
>> >
>> > I have over 30 initialization parameters that are
> accessed
>> > by the subfunctions, and the only way I can see of
> feeding
>> > that information to the simulink model is as inputs to
> the
>> > embedded matlab block. That seems really messy.
>> > I was wondering if there is a way to get these mfiles
> to
>> > see the parameters in the base workspace.
>> > I could run an initialization script to populate the
>> > workspace before running the simulink model... but I
> need
>> > this program to see those parameters.
>> >
>> > Got any ideas?
>> > Thank you in advance!
>> > -Elizabeth
>>
>>
>


Subject: Embedded Matlab and Parameters

From: elizabeth weitzel

Date: 1 Jul, 2008 22:00:21

Message: 5 of 9

R2007a is what I'm using.

Thank you,
Elizabeth
"Michael Hosea" <mhosea@mathworks.com> wrote in message
<g4e8ac$9d3$1@fred.mathworks.com>...
> It worked for me, but I'll have to try it in the release
that you're
> actually using. Which release is that?
> --
> Mike
>
> "elizabeth weitzel" <elizabeth.weitzel@baesystems.com>
wrote in message
> news:g4e5qm$9jp$1@fred.mathworks.com...
> > Yes, I run an init script that sets the parameters to
the
> > base workspace, but when I compile and run, it tells me
> > that the parameters are not defined and gives me links
to
> > the extrinsic functions.
> >
> >
> > "Michael Hosea" <mhosea@mathworks.com> wrote in message
> > <g4e3to$5fa$1@fred.mathworks.com>...
> >> Have you tried running your initialization script and
> > then running a
> >> Simulink model in which Embedded MATLAB blocks call
your
> > other scripts
> >> extrinsically? What (if anything) goes wrong? I
think,
> > whether or not one
> >> admires the system design, it should work as you
> > describe.
> >> --
> >> Mike
> >>
> >> "elizabeth weitzel" <elizabeth.weitzel@baesystems.com>
> > wrote in message
> >> news:g4dveq$qg4$1@fred.mathworks.com...
> >> > Hello,
> >> >
> >> > I am at a loss and could use a bit of advice from
those
> >> > more experienced than I.
> >> >
> >> > I have a program that needs to be run in a simulink
> > model,
> >> > and this program is composed of many mfile scripts.
I
> >> > understand I can use an embedded matlab function and
> > call
> >> > these mfiles extrinsically.. but the problem I have
is
> >> > with the initialization parameters.
> >> >
> >> > I have over 30 initialization parameters that are
> > accessed
> >> > by the subfunctions, and the only way I can see of
> > feeding
> >> > that information to the simulink model is as inputs
to
> > the
> >> > embedded matlab block. That seems really messy.
> >> > I was wondering if there is a way to get these
mfiles
> > to
> >> > see the parameters in the base workspace.
> >> > I could run an initialization script to populate the
> >> > workspace before running the simulink model... but I
> > need
> >> > this program to see those parameters.
> >> >
> >> > Got any ideas?
> >> > Thank you in advance!
> >> > -Elizabeth
> >>
> >>
> >
>
>

Subject: Embedded Matlab and Parameters

From: Michael Hosea

Date: 2 Jul, 2008 04:23:07

Message: 6 of 9

There could be some settings differences, but in 2007b I just ran a simple
model with an Embedded MATLAB block with the function:

    function y = fcn(u)
    eml.extrinsic('fooscript');
    fooscript;
    y = u;

where fooscript.m is

    %#eml
    a = a + 1

I intentionally left the semicolon off to see the statement executed.

If I fail to define the variable 'a' before running the model, I get the
error:

    ??? Undefined function or variable 'a'.

    Error in ==> fooscript at 2
    a = a + 1

But if I define a = 1 in the base workspace, and then run the model, it
works as expected--the variable 'a' is incremented several times.

I am thinking one of two things is going on. Either there is some setting
or circumstance that I don't know about which forces your particular
Simulink model to run in a different workspace, or the variables it is
complaining about really aren't initialized.
--
Mike


"elizabeth weitzel" <elizabeth.weitzel@baesystems.com> wrote in message
news:g4e9dl$obq$1@fred.mathworks.com...
> R2007a is what I'm using.
>
> Thank you,
> Elizabeth
> "Michael Hosea" <mhosea@mathworks.com> wrote in message
> <g4e8ac$9d3$1@fred.mathworks.com>...
>> It worked for me, but I'll have to try it in the release
> that you're
>> actually using. Which release is that?
>> --
>> Mike
>>
>> "elizabeth weitzel" <elizabeth.weitzel@baesystems.com>
> wrote in message
>> news:g4e5qm$9jp$1@fred.mathworks.com...
>> > Yes, I run an init script that sets the parameters to
> the
>> > base workspace, but when I compile and run, it tells me
>> > that the parameters are not defined and gives me links
> to
>> > the extrinsic functions.
>> >
>> >
>> > "Michael Hosea" <mhosea@mathworks.com> wrote in message
>> > <g4e3to$5fa$1@fred.mathworks.com>...
>> >> Have you tried running your initialization script and
>> > then running a
>> >> Simulink model in which Embedded MATLAB blocks call
> your
>> > other scripts
>> >> extrinsically? What (if anything) goes wrong? I
> think,
>> > whether or not one
>> >> admires the system design, it should work as you
>> > describe.
>> >> --
>> >> Mike
>> >>
>> >> "elizabeth weitzel" <elizabeth.weitzel@baesystems.com>
>> > wrote in message
>> >> news:g4dveq$qg4$1@fred.mathworks.com...
>> >> > Hello,
>> >> >
>> >> > I am at a loss and could use a bit of advice from
> those
>> >> > more experienced than I.
>> >> >
>> >> > I have a program that needs to be run in a simulink
>> > model,
>> >> > and this program is composed of many mfile scripts.
> I
>> >> > understand I can use an embedded matlab function and
>> > call
>> >> > these mfiles extrinsically.. but the problem I have
> is
>> >> > with the initialization parameters.
>> >> >
>> >> > I have over 30 initialization parameters that are
>> > accessed
>> >> > by the subfunctions, and the only way I can see of
>> > feeding
>> >> > that information to the simulink model is as inputs
> to
>> > the
>> >> > embedded matlab block. That seems really messy.
>> >> > I was wondering if there is a way to get these
> mfiles
>> > to
>> >> > see the parameters in the base workspace.
>> >> > I could run an initialization script to populate the
>> >> > workspace before running the simulink model... but I
>> > need
>> >> > this program to see those parameters.
>> >> >
>> >> > Got any ideas?
>> >> > Thank you in advance!
>> >> > -Elizabeth
>> >>
>> >>
>> >
>>
>>
>


Subject: Embedded Matlab and Parameters

From: Michael Hosea

Date: 2 Jul, 2008 04:24:55

Message: 7 of 9

Sorry, just a typo. I meant to say 2007a: Version 7.4.0.287, January 29,
2007.

"Michael Hosea" <mhosea@mathworks.com> wrote in message
news:g4evrb$2fp$1@fred.mathworks.com...
> There could be some settings differences, but in 2007b I just ran a simple


Subject: Embedded Matlab and Parameters

From: elizabeth weitzel

Date: 2 Jul, 2008 17:52:10

Message: 8 of 9

Ah I see my problem!
I can't operate on those values and pass the results back.

for instance:

EML:
function y = fcn(u)
% This block supports an embeddable subset of the MATLAB
language.
% See the help menu for details.
    eml.extrinsic('fooscript');
    a = 1; %set size a
    a = fooscript;
    y = u+a;

Fooscript:
function a = fooscript
a=a+1
end


Error:
Embedded MATLAB Interface Error: Undefined function or
variable "a".

Each of these scripts operates on values passed to one
another, and the parameters in the workspace.. the end
result needs to be fed back into simulink for another
model to work with. I guess they were seeing the
parameters all along... but the embedded matlab won't see
the results.

-Elizabeth


"Michael Hosea" <mhosea@mathworks.com> wrote in message
<g4evun$3h4$1@fred.mathworks.com>...
> Sorry, just a typo. I meant to say 2007a: Version
7.4.0.287, January 29,
> 2007.
>
> "Michael Hosea" <mhosea@mathworks.com> wrote in message
> news:g4evrb$2fp$1@fred.mathworks.com...
> > There could be some settings differences, but in 2007b
I just ran a simple
>
>

Subject: Embedded Matlab and Parameters

From: Michael Hosea

Date: 3 Jul, 2008 00:59:12

Message: 9 of 9

It's slightly inconvenient for me to switch back to 2007a, so I just tried
the following in my current version. If you find that things work
differently for you, let me know.

When you changed fooscript from an M script to an M function, the variable
'a' automatically became local in scope, so you'll have to declare the
variable 'a' global, first. From the command line

>> global a
>> a = 0

Then your extrinsic function looks something like

    function y = foofun
    global a
    a = a + 1;
    y = a;

and the Embedded MATLAB function might look something like

    function y = fcn(u)
    eml.extrinsic('foofun');
    x = 0;
    x = foofun;
    y = x + u;

Quite generally you must sort out where variables are stored. In M
functions the default scope is local, so if you want to share a variable,
you must declare it global (in both places). Embedded MATLAB function
variables should always be considered local to Embedded MATLAB (we don't
support global variables). As far as a know, MATLAB M scripts, on the other
hand, behave as if you were typing it all in at the command line (which is
why scripts are so very difficult to maintain).

BTW, I just happened to notice and wanted to mention that I shouldn't have
included the %#eml in fooscript.m, only because it's irrelevant with
anything extrinsic, let alone an M script. I just added it out of force of
habit. It's supposed to indicate that an M function is Embedded MATLAB
compliant.
--
Mike


"elizabeth weitzel" <elizabeth.weitzel@baesystems.com> wrote in message
news:g4gf8a$maj$1@fred.mathworks.com...
> Ah I see my problem!
> I can't operate on those values and pass the results back.
>
> for instance:
>
> EML:
> function y = fcn(u)
> % This block supports an embeddable subset of the MATLAB
> language.
> % See the help menu for details.
> eml.extrinsic('fooscript');
> a = 1; %set size a
> a = fooscript;
> y = u+a;
>
> Fooscript:
> function a = fooscript
> a=a+1
> end
>
>
> Error:
> Embedded MATLAB Interface Error: Undefined function or
> variable "a".
>
> Each of these scripts operates on values passed to one
> another, and the parameters in the workspace.. the end
> result needs to be fed back into simulink for another
> model to work with. I guess they were seeing the
> parameters all along... but the embedded matlab won't see
> the results.
>
> -Elizabeth
>
>
> "Michael Hosea" <mhosea@mathworks.com> wrote in message
> <g4evun$3h4$1@fred.mathworks.com>...
>> Sorry, just a typo. I meant to say 2007a: Version
> 7.4.0.287, January 29,
>> 2007.
>>
>> "Michael Hosea" <mhosea@mathworks.com> wrote in message
>> news:g4evrb$2fp$1@fred.mathworks.com...
>> > There could be some settings differences, but in 2007b
> I just ran a simple
>>
>>
>


Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
embedded matlab elizabeth weitzel 1 Jul, 2008 15:17:32
workspace elizabeth weitzel 1 Jul, 2008 15:17:32
simulink elizabeth weitzel 1 Jul, 2008 15:17:32
rssFeed for this Thread

Public Submission Policy

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.

Contact us at files@mathworks.com