reading a workspace value inside a function

I used xlsread in a function. now I have another function that uses this function, and that function is used by a script. So xlsread is used 100 of times uselessly and I am getting 'out of memory' error (this maybe is not the main cause of the error). if I take xlsread into the script, the function cannot read the read variable, even it is in the workspace. How do I make the function to use the variable, without making it input variable of the function?
thanks

10 commentaires

Stephen23
Stephen23 le 11 Juil 2019
"without making it input variable of the function?"
Why not?
Passing data as input/output arguments is the simplest and most efficient way of passing data between workspaces.
Asliddin Komilov
Asliddin Komilov le 11 Juil 2019
Modifié(e) : Asliddin Komilov le 11 Juil 2019
I will have to do it for the all functions above on hierarchy, I would rather try/learn some other way if its there )
Stephen23
Stephen23 le 11 Juil 2019
Modifié(e) : Stephen23 le 11 Juil 2019
"I will have to do it for the all functions above on hierarchy..."
Possibly, but without seeing an architecture or the code we can only guess what you are doing.
"...I would rather try/learn some other way if its there"
There are always multiple ways of doing things: you could use nested functions.
In general it looks like this:
Script:
A=0:1300;
for i=1:10:length(B)
[]=function1()
end
Function1:
B=0:133;
for i=1:length(B)
[]=function2()
end
Function2:
C= xlsread()
D=1:129
for i=1:length(D)
Variables (c)
[]=function3(Variables (c))
end
So C is read 130x133 times.
Asliddin Komilov
Asliddin Komilov le 12 Juil 2019
sorry couldn't figure out how to use them in my code (
persistent xlsread_memoized
if isempty(xlsread_memoized)
xlsread_memoized = memoize(@xlsread) ;
end
C = xlsread_memoized(filename);
As long as the filename does not change, the previously read values will be pulled from memory instead of re-reading the file. This would be through the typical copy-on-write mechanism so additional memory would not be needed.
Asliddin Komilov
Asliddin Komilov le 13 Juil 2019
Modifié(e) : Asliddin Komilov le 13 Juil 2019
thanks,
somehow my 2016a did not have memoize.m so I got it from internet.
it gave me this error:
Undefined function or variable 'identity'.
Error in memoize/inner (line 49)
argskey = keyfun(varargin);
for the C = xlsread_memoized(filename),
and I used momoizefun.m but the difference in run time with and without memoize is not so much:
Elapsed time is 4.904682 seconds. (with)
Elapsed time is 2.154016 seconds. (without)
Elapsed time is 0.916771 seconds.
Elapsed time is 1.406389 seconds.
Elapsed time is 1.042952 seconds.
Elapsed time is 1.537564 seconds.
Elapsed time is 1.726902 seconds.
Elapsed time is 1.381856 seconds.
TADA
TADA le 13 Juil 2019
Modifié(e) : TADA le 13 Juil 2019
Yes, unfortunately, memoize was introduced in 2017a.
Actually you don't need to keep a MemoizedFunction object as a persistent variable. memoize manages its own cache persistence.
Are you reading the same file every time?
Asliddin Komilov
Asliddin Komilov le 14 Juil 2019
Modifié(e) : Asliddin Komilov le 14 Juil 2019
yes its the same file, it would be best if I could do it inside the script and make other functions use it. I am not willing to put it in function input because there are many variables used by different functions. But it is already good without 'persistent':
Elapsed time is 0.077645 seconds.
Elapsed time is 1.035841 seconds.
Elapsed time is 0.005850 seconds.
Elapsed time is 1.412184 seconds.
Elapsed time is 0.000608 seconds.
Elapsed time is 1.529667 seconds.
Elapsed time is 0.001081 seconds.
Elapsed time is 1.416243 seconds.
Elapsed time is 0.000831 seconds.
Elapsed time is 1.475994 seconds.
thanks

Connectez-vous pour commenter.

 Réponse acceptée

Asliddin Komilov
Asliddin Komilov le 19 Août 2019

0 votes

the solution was the momoizefun, look for details above.

Plus de réponses (0)

Catégories

En savoir plus sur File Operations dans Centre d'aide et File Exchange

Produits

Version

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by