From: CyberFrog on
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
From: James Tursa on
"CyberFrog" <domlee55(a)hotmail.com> wrote in message <i0scer$i3c$1(a)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
From: CyberFrog on
"James Tursa" <aclassyguy_with_a_k_not_a_c(a)hotmail.com> wrote in message <i0shpu$358$1(a)fred.mathworks.com>...
> "CyberFrog" <domlee55(a)hotmail.com> wrote in message <i0scer$i3c$1(a)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?
From: Sadik on
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.
From: CyberFrog on
"Sadik " <sadik.hava(a)gmail.com> wrote in message <i0tjbr$nr5$1(a)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
 |  Next  |  Last
Pages: 1 2
Prev: 3d matrix array plot
Next: Mex and gsl