Thread Subject: How to read workspace variable from Simulink?

Subject: How to read workspace variable from Simulink?

From: Daniel

Date: 20 Oct, 2009 02:23:03

Message: 1 of 3

I need to use a SimEvents Attribute Function to read the value of a variable in the MATLAB workspace. I'm able to write to it by using the following inside the Attribute Function:

    eml.extrinsic('assignin');
    assignin('base','someVar',42);

This works fine inside the Attribute Function. The MATLAB workspace variable called 'someVar' gets set to 42. But when I try to do the same with 'evalin' like this:

    eml.extrinsic('evalin');
    if evalin('base','someVar') == 42
         ....(other stuff)...

I get an error at "evalin('base',someVar')" that says "Expected either a logical, char, int, fi, single, or double. Found a MATLAB type. MATLAB types are returned from calls to the MATLAB interpreter and are not supported inside expressions. They may be used on the right-hand side of assignments and as arguments to MATLAB calls."

What must I do to be able to read the workspace variable I was just able to write from inside an Attribute Function?

Subject: How to read workspace variable from Simulink?

From: Devdatt Lad

Date: 20 Oct, 2009 15:09:55

Message: 2 of 3

Hi Daniel,

Use the following approach with EVALIN:

eml.extrinsic('evalin')
x = 0;
x = evalin('base', 'someVar;');
if x == 42
   ... other stuff
end

Why do this?

When you use eml.extrinsic, you are telling the EML compiler to not generate
code for the function 'evalin', but rather, dispatch that call to MATLAB.
Now, MATLAB returns variables of type 'mxArray'. Thus, the return value from
'evalin' is of type 'mxArray'. EML cannot compare an 'mxArray' to the double
number 42 and hence throws an error.

To resolve this, the return value from 'evalin' should be assigned to a
variable whos type is already known to the EML compiler. Thus, by saying: x
= 0; you are telling the EML compiler that the variable 'x' has a type
'double' and value 0.

Now, changing the value of x by saying:

    x = evalin(...)

does not change the type of x which continues to remain 'double' and now you
can use this variable in expressions.

See the following doc page for more information:
http://www.mathworks.com/access/helpdesk/help/toolbox/eml/ug/bq1h2z8-34.html#bq1h2z9-38

Hope this helps,
Devdatt

---
Devdatt Lad
Discrete-Event Simulation
The MathWorks, Inc.


"Daniel " <ddeibler0001@email.vccs.edu> wrote in message
news:hbj6u7$n90$1@fred.mathworks.com...
>I need to use a SimEvents Attribute Function to read the value of a
>variable in the MATLAB workspace. I'm able to write to it by using the
>following inside the Attribute Function:
>
> eml.extrinsic('assignin');
> assignin('base','someVar',42);
>
> This works fine inside the Attribute Function. The MATLAB workspace
> variable called 'someVar' gets set to 42. But when I try to do the same
> with 'evalin' like this:
>
> eml.extrinsic('evalin');
> if evalin('base','someVar') == 42
> ....(other stuff)...
>
> I get an error at "evalin('base',someVar')" that says "Expected either a
> logical, char, int, fi, single, or double. Found a MATLAB type. MATLAB
> types are returned from calls to the MATLAB interpreter and are not
> supported inside expressions. They may be used on the right-hand side of
> assignments and as arguments to MATLAB calls."
>
> What must I do to be able to read the workspace variable I was just able
> to write from inside an Attribute Function?

Subject: How to read workspace variable from Simulink?

From: Daniel

Date: 21 Oct, 2009 15:38:19

Message: 3 of 3

Oh my goodness, it actually works. Thanks a million!
"Devdatt Lad" <dlad@mathworks.com> wrote in message <hbkjs3$d9e$1@fred.mathworks.com>...
> Hi Daniel,
>
> Use the following approach with EVALIN:
>
> eml.extrinsic('evalin')
> x = 0;
> x = evalin('base', 'someVar;');
> if x == 42
> ... other stuff
> end
>
> Why do this?
>
> When you use eml.extrinsic, you are telling the EML compiler to not generate
> code for the function 'evalin', but rather, dispatch that call to MATLAB.
> Now, MATLAB returns variables of type 'mxArray'. Thus, the return value from
> 'evalin' is of type 'mxArray'. EML cannot compare an 'mxArray' to the double
> number 42 and hence throws an error.
>
> To resolve this, the return value from 'evalin' should be assigned to a
> variable whos type is already known to the EML compiler. Thus, by saying: x
> = 0; you are telling the EML compiler that the variable 'x' has a type
> 'double' and value 0.
>
> Now, changing the value of x by saying:
>
> x = evalin(...)
>
> does not change the type of x which continues to remain 'double' and now you
> can use this variable in expressions.
>
> See the following doc page for more information:
> http://www.mathworks.com/access/helpdesk/help/toolbox/eml/ug/bq1h2z8-34.html#bq1h2z9-38
>
> Hope this helps,
> Devdatt
>
> ---
> Devdatt Lad
> Discrete-Event Simulation
> The MathWorks, Inc.
>
>
> "Daniel " <ddeibler0001@email.vccs.edu> wrote in message
> news:hbj6u7$n90$1@fred.mathworks.com...
> >I need to use a SimEvents Attribute Function to read the value of a
> >variable in the MATLAB workspace. I'm able to write to it by using the
> >following inside the Attribute Function:
> >
> > eml.extrinsic('assignin');
> > assignin('base','someVar',42);
> >
> > This works fine inside the Attribute Function. The MATLAB workspace
> > variable called 'someVar' gets set to 42. But when I try to do the same
> > with 'evalin' like this:
> >
> > eml.extrinsic('evalin');
> > if evalin('base','someVar') == 42
> > ....(other stuff)...
> >
> > I get an error at "evalin('base',someVar')" that says "Expected either a
> > logical, char, int, fi, single, or double. Found a MATLAB type. MATLAB
> > types are returned from calls to the MATLAB interpreter and are not
> > supported inside expressions. They may be used on the right-hand side of
> > assignments and as arguments to MATLAB calls."
> >
> > What must I do to be able to read the workspace variable I was just able
> > to write from inside an Attribute Function?
>

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
assignin Daniel 19 Oct, 2009 22:24:04
evalin Daniel 19 Oct, 2009 22:24:04
attribute function Daniel 19 Oct, 2009 22:24:04
workspace variable Daniel 19 Oct, 2009 22:24:04
rssFeed for this Thread

Contact us at files@mathworks.com