reading a workspace value inside a function
Show older comments
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
Stephen23
on 11 Jul 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
on 11 Jul 2019
Edited: Asliddin Komilov
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.
Asliddin Komilov
on 12 Jul 2019
Asliddin Komilov
on 12 Jul 2019
Walter Roberson
on 12 Jul 2019
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
on 13 Jul 2019
Edited: Asliddin Komilov
on 13 Jul 2019
Asliddin Komilov
on 14 Jul 2019
Edited: Asliddin Komilov
on 14 Jul 2019
Accepted Answer
More Answers (0)
Categories
Find more on File Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!