Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
one simple question

Subject: one simple question

From: ghasem

Date: 25 Apr, 2013 07:53:08

Message: 1 of 4

Hi.
if I run two different matlab program with several similar parameters and subroutines,in two separate matlab windows simultaneously,(with addpath(genpath(...))), these similar parameters and subroutines have effect on other program?
note that each full program is stored in separate folder in computer.
for example :
in both code I have "a" parameter,such that in first program a=10 and in second a=20.
also,in both program I have " wh34_matlab.m" subroutine.
now,when run these programs simultaneously,a=10 in first program, has effect on "a" value in second program?
or,similarly,"details of wh34_matlab" subroutine has effect on "wh34_matlab" in other program?
i.e,Values ??intersection does not occur?
ghasem

Subject: one simple question

From: Nasser M. Abbasi

Date: 25 Apr, 2013 11:45:52

Message: 2 of 4

On 4/25/2013 2:53 AM, ghasem wrote:

> now,when run these programs simultaneously,a=10 in first program,
>has effect on "a" value in second program?

You should read this:

http://www.mathworks.com/help/matlab/matlab_prog/base-and-function-workspaces.html

"Functions do not use the base workspace. Every function has
its own function workspace. Each function workspace is separate
from the base workspace and all other workspaces to protect the integrity of the data"

--Nasser

Subject: one simple question

From: ghasem

Date: 25 Apr, 2013 12:35:10

Message: 3 of 4

> "Functions do not use the base workspace. Every function has
> its own function workspace. Each function workspace is separate
> from the base workspace and all other workspaces to protect the integrity of the data"
>
> --Nasser
========================
So,they don't effect on each other?
I must to note that I didn't create function.in fact I use from several subroutines that are call in one main program and by addpath(genpath('folder path')),I introduce subroutines path to matlab.
I don't write a function,but used from script.

Subject: one simple question

From: Nasser M. Abbasi

Date: 26 Apr, 2013 00:58:36

Message: 4 of 4

On 4/25/2013 7:35 AM, ghasem wrote:
>> "Functions do not use the base workspace. Every function has
>> its own function workspace. Each function workspace is separate
>> from the base workspace and all other workspaces to protect the integrity of the data"
>>
>> --Nasser
> ========================

> So,they don't effect on each other?

correct

> I must to note that I didn't create function.in fact I use from
>several subroutines that are call in one main program and by
>addpath(genpath('folder path')),I introduce subroutines path to matlab.
> I don't write a function,but used from script.
>

It does not matter. Once a function returns, any variable
it introduces inside it, will disappear after the function
returns.

Unless you are explicitly using global variables (i.e. using
shared variables, `help global`), then a function can only be
interfaced with via the calling parameters and the return
variables, it returns, if any.

when you write

function foo(a,b)

x=1
....
end

This 'x' is gone for ever when the function returns. Another function
doing the same will use a different 'x', local to it.

This is not different really than any standard programming
language. There are local variables and there are global
variables and there are parameters, which are values
passed between functions and there are function return values.

Unless you use Classes in Matlab, there are the only things
you need to worry about.

And do not use shared global variables, they cause problems.

--Nasser

Tags for this Thread

No tags are associated with this thread.

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.

Contact us