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:
??? Reference to a cleared variable

Subject: ??? Reference to a cleared variable

From: CyberFrog

Date: 5 Jul, 2010 10:34:03

Message: 1 of 6

Hi,

I have 2 moules of code whereby module_A feeds in certain inputs to module_B so each function id defined as:

function module_A
within this module the call to module_B is simply:
module_B(input1, input2, input3)

function module_B(input1, input2, input3)

As you can see to feed in these inputs into a seconf module is easier enough but I do not know how to return these any new inputs generated by module_B into module_A??? Assume the call in A occurs half way down a load of code therefore I would like to feed in new inpust generated from B into A but to then continue down the second hald of code in A. Obviously I cannot use this same call because it will start the code again when I just want it to continue.

Thanks

Subject: ??? Reference to a cleared variable

From: James Tursa

Date: 5 Jul, 2010 12:05:18

Message: 2 of 6

"CyberFrog" <domlee55@hotmail.com> wrote in message <i0scer$i3c$1@fred.mathworks.com>...
> Hi,
>
> I have 2 moules of code whereby module_A feeds in certain inputs to module_B so each function id defined as:
>
> function module_A
> within this module the call to module_B is simply:
> module_B(input1, input2, input3)
>
> function module_B(input1, input2, input3)
>
> As you can see to feed in these inputs into a seconf module is easier enough but I do not know how to return these any new inputs generated by module_B into module_A??? Assume the call in A occurs half way down a load of code therefore I would like to feed in new inpust generated from B into A but to then continue down the second hald of code in A. Obviously I cannot use this same call because it will start the code again when I just want it to continue.


If I understand you correctly, just do this:

function [input1 input2 input2] = module_B(input1, input2, input3)
    :
end

and then change the call to module_B accordingly.

James Tursa

Subject: ??? Reference to a cleared variable

From: CyberFrog

Date: 5 Jul, 2010 15:17:04

Message: 3 of 6

"James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <i0shpu$358$1@fred.mathworks.com>...
> "CyberFrog" <domlee55@hotmail.com> wrote in message <i0scer$i3c$1@fred.mathworks.com>...
> > Hi,
> >
> > I have 2 moules of code whereby module_A feeds in certain inputs to module_B so each function id defined as:
> >
> > function module_A
> > within this module the call to module_B is simply:
> > module_B(input1, input2, input3)
> >
> > function module_B(input1, input2, input3)
> >
> > As you can see to feed in these inputs into a seconf module is easier enough but I do not know how to return these any new inputs generated by module_B into module_A??? Assume the call in A occurs half way down a load of code therefore I would like to feed in new inpust generated from B into A but to then continue down the second hald of code in A. Obviously I cannot use this same call because it will start the code again when I just want it to continue.
>
>
> If I understand you correctly, just do this:
>
> function [input1 input2 input2] = module_B(input1, input2, input3)
> :
> end
>
> and then change the call to module_B accordingly.
>
> James Tursa

Yep this seemed to work although my understanding of why is a bit lacking, could you explain the reason behind?

Subject: ??? Reference to a cleared variable

From: Sadik

Date: 5 Jul, 2010 21:38:03

Message: 4 of 6

Hi CyberFrog,

This worked because matlab doesn't pass variables as reference. Put differently, when you pass a variable to a matlab function, it is simply copied for the scope of module_B and nothing happens to the original variables in the scope of module_A.

When you instead do

[input1,input2,input3] = module_B(input1,input2,input3);

you are now assigning the values of those new copies modified in scope B to the original ones in scope A.

If you need more info about how it works from a programming point of view, you could search the net for

pass by value
pass by reference

Best.

Subject: ??? Reference to a cleared variable

From: CyberFrog

Date: 6 Jul, 2010 06:54:05

Message: 5 of 6

"Sadik " <sadik.hava@gmail.com> wrote in message <i0tjbr$nr5$1@fred.mathworks.com>...
> Hi CyberFrog,
>
> This worked because matlab doesn't pass variables as reference. Put differently, when you pass a variable to a matlab function, it is simply copied for the scope of module_B and nothing happens to the original variables in the scope of module_A.
>
> When you instead do
>
> [input1,input2,input3] = module_B(input1,input2,input3);
>
> you are now assigning the values of those new copies modified in scope B to the original ones in scope A.
>
> If you need more info about how it works from a programming point of view, you could search the net for
>
> pass by value
> pass by reference
>
> Best.

Thanks Sadik your explanation helps

Subject: ??? Reference to a cleared variable

From: James Tursa

Date: 6 Jul, 2010 07:27:05

Message: 6 of 6

"Sadik " <sadik.hava@gmail.com> wrote in message <i0tjbr$nr5$1@fred.mathworks.com>...
>
> This worked because matlab doesn't pass variables as reference. Put differently, when you pass a variable to a matlab function, it is simply copied for the scope of module_B and nothing happens to the original variables in the scope of module_A.

Not quite. When MATLAB passes arguments from module_A to an m-file function module_B, it passes a shared data copy of the argument (new header information but points to same data) to the module_B function. If the module_B function treats the argument as read-only then nothing else is done to the variable. But if the module_B function changes the variable, then a copy of the data is made (maybe) at that time for the module_B variable and then the changes are applied to this new data area (and nothing happens to the original variable in the scope of module_A). The reason I say maybe is that under certain circumstances (later version of MATLAB, calling the module_B function from within another function, and passing out the input variable as the same output variable) then a copy of the data is not made. For most practical purposes, MATLAB can be regarded as pass by value for m-files and
intrinsic functions but internally it doesn't really work that way.

When MATLAB passes arguments to a mex function, it passes the reference to the original variable ... there is no copy (shared or deep) made. So if module_B function is a mex function and it changes the argument, it will change the original variable in the scope of module_A.

James Tursa

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