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 Comments

"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.
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 on 11 Jul 2019
Edited: Stephen23 on 11 Jul 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.
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.
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 on 13 Jul 2019
Edited: TADA on 13 Jul 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?
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

Sign in to comment.

 Accepted Answer

the solution was the momoizefun, look for details above.

More Answers (0)

Categories

Products

Release

R2016a

Community Treasure Hunt

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

Start Hunting!