Azzera filtri
Azzera filtri

How to avoid repeating load data from a function

6 visualizzazioni (ultimi 30 giorni)
Yang
Yang il 11 Feb 2012
Modificato: Abhay Mohan il 9 Ott 2013
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?

Risposte (1)

Walter Roberson
Walter Roberson il 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 Commenti
Yang
Yang il 13 Feb 2012
Thanks for suggestions. But my function is an outside standalone function and I'm not going to add it as a nested function inside another function.
This is my function that to be looped:
function select = recognition( f )
%RECONGNITE Summary of this function goes here
% Detailed explanation goes here
load characters.mat;
load cluster_record2.mat;
load space_variance.mat;
load feature.mat;
load mean_variance.mat;
load level_number.mat;
......
end
and another function to call this function. If I put load sentences in the outside function, data will not pass into the called function.
Walter Roberson
Walter Roberson il 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.

Accedi per commentare.

Categorie

Scopri di più su Structures in Help Center e File Exchange

Tag

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by