Thread Subject: Extracting a value from a script file

Subject: Extracting a value from a script file

From: Milos Milenkovic

Date: 9 Feb, 2012 08:49:12

Message: 1 of 4

Dear,
simply question. If I have certain outputs in different m-script files, how to use it in a separate script, or in command window?
For example, in Milos1 I have result H (as a matrix), in Milos 2 - result P. I want to use these two matrices for compute N in command window or as a part of third script file.

Best,
Milos

Subject: Extracting a value from a script file

From: Pekka Kumpulainen

Date: 9 Feb, 2012 11:26:10

Message: 2 of 4

"Milos Milenkovic" <m.milenkovic@mathworks.com> wrote in message <jh01a7$g4h$1@newscl01ah.mathworks.com>...
> Dear,
> simply question. If I have certain outputs in different m-script files, how to use it in a separate script, or in command window?
> For example, in Milos1 I have result H (as a matrix), in Milos 2 - result P. I want to use these two matrices for compute N in command window or as a part of third script file.
>
> Best,
> Milos

Scripts evaluate in the main workspace. All the variables created in your scripts are available on the command line and in any other script you run from the command line.
After running Milos1 and Milos2 your workspace should contain H and P and you can use them.
As long as you are really using scripts, not functions. You might still want to read section "Scripts and Functions" from Getting Started -> Programming in the documentation.
hth

Subject: Extracting a value from a script file

From: Milos Milenkovic

Date: 9 Feb, 2012 11:45:15

Message: 3 of 4

Dear Pekka,
thank you!!
"Pekka Kumpulainen" <pekka.nospam.kumpulainen@tut.please.fi> wrote in message <jh0agi$cu8$1@newscl01ah.mathworks.com>...
> "Milos Milenkovic" <m.milenkovic@mathworks.com> wrote in message <jh01a7$g4h$1@newscl01ah.mathworks.com>...
> > Dear,
> > simply question. If I have certain outputs in different m-script files, how to use it in a separate script, or in command window?
> > For example, in Milos1 I have result H (as a matrix), in Milos 2 - result P. I want to use these two matrices for compute N in command window or as a part of third script file.
> >
> > Best,
> > Milos
>
> Scripts evaluate in the main workspace. All the variables created in your scripts are available on the command line and in any other script you run from the command line.
> After running Milos1 and Milos2 your workspace should contain H and P and you can use them.
> As long as you are really using scripts, not functions. You might still want to read section "Scripts and Functions" from Getting Started -> Programming in the documentation.
> hth

Subject: Extracting a value from a script file

From: Steven_Lord

Date: 9 Feb, 2012 15:30:11

Message: 4 of 4



"Pekka Kumpulainen" <pekka.nospam.kumpulainen@tut.please.fi> wrote in
message news:jh0agi$cu8$1@newscl01ah.mathworks.com...
> "Milos Milenkovic" <m.milenkovic@mathworks.com> wrote in message
> <jh01a7$g4h$1@newscl01ah.mathworks.com>...
>> Dear,
>> simply question. If I have certain outputs in different m-script files,
>> how to use it in a separate script, or in command window? For example, in
>> Milos1 I have result H (as a matrix), in Milos 2 - result P. I want to
>> use these two matrices for compute N in command window or as a part of
>> third script file. Best,
>> Milos
>
> Scripts evaluate in the main workspace.

*snip*

Scripts only work in the base workspace if you call them from the command
line. If you call them from within a function, they operate in the workspace
of that function. You can check this by creating a variable in the base
workspace, a script that assigns a value to that variable, and a function
that calls the script, like:

% begin myscript.m
x = 1;
% end myscript.m

% begin myFunctionCallingScript.m
function y = myFunctionCallingScript(x)
myscript;
y = x+1;
% end myFunctionCallingScript.m

Now at the command prompt, type:

x = 23;
y = myFunctionCallingScript(72)

The base workspace's variable x will still be 23 (the script will not have
overwritten it) and y will be 2 (because the local variable x inside
myFunctionCallingScript will have been overwritten.)

Yes, I know that the documentation page says something different:

http://www.mathworks.com/help/techdoc/matlab_prog/f7-38085.html

I think the intention of the sentence "Scripts share the base workspace with
your interactive MATLAB session and with other scripts." was to clarify that
there is only one base workspace, and if you execute two separate scripts
they won't work in their own copy of the base workspace or anything like
that. I will inform the documentation staff so they can clarify that
statement.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

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.

rssFeed for this Thread

Contact us at files@mathworks.com