retrieve a value from another file
Ältere Kommentare anzeigen
I have a file, we'll call it "base.m", that contains many lines of code. I also have another file called "alter.m". Within "alter.m" I want to retrieve an array, called "array", from "base.m". I want to repeat this process, except instead of "base.m", I will use "alter.m"; and instead of "alter.m", I will have another file called "final.m". Basically, I want to send information(an array) from base.m to alter.m, then information(an array) from alter.m to final.m . Suggestions on how to approach this? Function? Or is there a simpler way?
1 Kommentar
Walter Roberson
am 27 Dez. 2011
Your duplicate question has been deleted.
Antworten (1)
Jan
am 26 Dez. 2011
Simpler than what?
Function base.m:
function base
array = rand(8, 9);
alter(array);
function alter(array)
final(array)
function final(array)
disp(array)
Does this match your needs?
6 Kommentare
Jaclyn Nord
am 26 Dez. 2011
Walter Roberson
am 26 Dez. 2011
Jan's code is close to the needs you indicated, but not quite the same. His code is for invoking alter from base, whereas you wanted to invoke base from alter.
function array = base
array = rand(8, 9);
function alter
array = base();
newarray = array * 1.23 + 4.56;
final(newarray)
function final(array)
disp(array)
You do not need to use functions at all: you could use scripts instead, relying on the fact that scripts work in the base workspace unless you call a script from a function.
As alter is your controlling routine, it would not need to be a function, taking in to consideration that separate files are being used. (Matters would be different if everything was in one file.)
Jaclyn Nord
am 26 Dez. 2011
Walter Roberson
am 27 Dez. 2011
With that sequence of calls, you need to invoke base instead of calling final.
%test.m
base
Jaclyn Nord
am 27 Dez. 2011
Walter Roberson
am 27 Dez. 2011
base calls upon alter which calls upon final. This is done by coding "alter" within "base" and coding "final" within "alter".
There is no mechanism to drive things the other way around, to call upon "final" and to have the system check back and notice that the only thing that calls "final" is "alter" so queue that to go first, and then figure out that the only thing that calls "alter" is "base" and so add that to the front of the list -- this is NOT done, there is no mechanism like that.
Consider that I could write
%base2.m
array=[3,1,4,1]
alter(array)
Then if I invoke base2 there is no problem because it can look and see the call to "alter" right there, and the code for "alter" has its call to "final" right there, so the flow base2 -> alter -> final is unambiguous.
But suppose for a moment that it did work in reverse, that you call upon final(). Okay, so "alter" is the only thing that calls upon that. But now, having back-tracked to "alter", how would alter be able to tell whether it was base.m or base2.m that needed to be invoked first in order to supply the call to "alter" ?
The same routine can be called by different files. That is not a problem if you work from the first file in the chain and look to see what it runs, and then what is run by that, and so on. Working backwards to decide which place calls a given routine does not produce useable results in such a case.
Kategorien
Mehr zu Operators and Elementary Operations finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!