How to avoid repeating load data from a function
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
dear all,
I have a function that will be call 1000 times. In that function, there are several "load" sentence to load some data. And I have another function to call this function 1000times. The data will be loaded 1000 times. If I put the load to the outside function, actually the data will not go into the inside function. How can I just load once and loop the function 1000 times?
0 Kommentare
Antworten (1)
Walter Roberson
am 11 Feb. 2012
Shared variables with a nested routine; or pass the values in to the routine.
Note that shared variables must be initialized (to any value) before the nested routine is declared.
function ndemo
foo = []; %okay to share
bar = []; %okay to share
function nested
biz = foo + bar; %okay, references previously-declared vars
baz = biff; %not okay, this will not exist until run time
end
biff = []; %not okay to share with "nested"
load fingle.mat; %load values for foo and bar and biff
[foo, bar, biff] %will show the loaded values
nested(); %will error saying biff was unknown
end
To emphasize: it isn't enough for the value of a variable to be known at execution time in order for it to be shared with a nested function: the variable has to have been given some visible value before the nested routine was declared, not before it was executed.
2 Kommentare
Walter Roberson
am 13 Feb. 2012
Then I suggest using "persistent".
persistent characters
persistent space_variance
and so on. Then have a test such as
if isempty(characters)
load characters.mat;
load cluster_record2.mat;
load space_variance.mat;
%etc
end
This will only do the load if the data has not already been loaded.
Siehe auch
Kategorien
Mehr zu Structures finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!