Is it possible to pass all variables of current workspace into a function without using 'load'
Show older comments
Is it possible to pass all variables of currently workspace into a function without using 'load'?
for example there are many variables in workspace like under:
a,b,c,d,e,f,g,... (more than 600 variables)
now is it possible by calling function ' all_workspace' all of a,b,c,d,e,f,g comes into function? but not with this:
function all_workspace
load workspace.mat %workspace.mat has saved before calling this function
but speed of this (using load) is too slow to use for 600 variables. also is it possible to share current base workspace between command window and function?
Accepted Answer
More Answers (4)
Jan
on 23 Sep 2011
2 votes
I've seen a lot of programs now, but I've never seen a reason to create 600 Variables. I'm convinced, that it is absolutely impossible to debug a program with such a bunch of variables. The idea of even sharing them with another function sound dangerous.
I suggest to use structures and cells to organize the data and to use subfunctions, which operate on the minimal set of variables only.
13 Comments
mohammad
on 23 Sep 2011
mohammad
on 23 Sep 2011
Walter Roberson
on 23 Sep 2011
You do not need one variable for each xls file: use a cell array instead.
mohammad
on 23 Sep 2011
Walter Roberson
on 23 Sep 2011
http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F
mohammad
on 23 Sep 2011
Walter Roberson
on 23 Sep 2011
The point is that the link shows you how to use cell arrays instead of individual variables. Quoting that:
====
In case each Ai contains a vector or matrix, each with a different size, you want to use cell arrays, which are intended exactly for this:
for i=1:10
A{i} = 1:i;
end
Note that each A{i} contains a different size matrix. And be careful to use the curly braces for the subscript!
====
Then you would only have the "A" matrix to pass around, not 600 individual variables. Or at most your "A" matrix and a cell array of strings in order to know which index corresponds to which original xls file name.
mohammad
on 23 Sep 2011
mohammad
on 23 Sep 2011
Walter Roberson
on 23 Sep 2011
You do not need to use cell2mat() to convert a single entry to a data matrix. If you stored a matrix in A{i}, then A{i} returns the matrix.
Are all of your files *exactly* the same size? If so, then you could be using a 3D array instead of a cell array.
mohammad
on 23 Sep 2011
Walter Roberson
on 23 Sep 2011
If you were to initialize
numfiles = 600;
filerows = 10000;
filecols = 2;
A = zeros(filerows,filecols,numfiles);
Then as you read in the K'th file, chop off any rows of data beyond #filerows, or (if necessary, just-in-case), pad it out to filerows (perhaps with nan.) Once you have your shortened or padded data, say in variable "filedata", you would use
A(:,:,K) = filedata;
(again, K is the number of the file.)
The end result after your reading will be a 3D array occupying somewhere around 92 megabytes of memory. It could then be accessed as (e.g.) A(:,1,23) to mean all the rows of column 1 of file #23, or A(:,:,23) to mean all of file #23.
Plotting the first column of file #17 on the same graph as the first column of file #75 would be plot(squeeze(A(:,1,[17 75])) or (effectively the same) plot([A(:,1,17), A(:,1,75)])
You could, if you had reason to, calculate across all of the files simultaneously. For example, mean(A(:,2,:)) would calculate the means of all of the second columns of all of the files, leaving a 1 x 1 x numfiles array of the means.
mohammad
on 23 Sep 2011
Daniel Shub
on 23 Sep 2011
1 vote
There are a number of different ugly hacks for doing this. A better approach is instead of having 600 variables in your workspace create a single structure with 600 fields and pass this structure.
3 Comments
mohammad
on 23 Sep 2011
Daniel Shub
on 23 Sep 2011
A starting place in the manual is: http://www.mathworks.com/help/releases/R2011a/techdoc/matlab_prog/br04bw6-38.html
Mohammad, I would say you are no longer a beginner in MATLAB. Your questions have definitely evolved over the past month or so. If you can make the time I would highly suggest you re-read the getting started part of the manual (http://www.mathworks.com/help/releases/R2011a/techdoc/learn_matlab/bqr_2pl.html) and then re-examine all the code you have written. As you re-examine the code you might find things that you can clean up. You will also find places that you need to add comments. You might even find segments that should be split off into small reusable and more manageable functions (see Jan's answer).
mohammad
on 23 Sep 2011
braa
on 16 Apr 2016
1 vote
i really hate this about matlab; there is no direct way to pass all workspace variables to a function. MATLAB leaves you no option but using bad logic. for me, i go around this by: right before calling the function i want, save all workspace variables to a .MAT file using the save command. call the function first line in the function: load the .MAT file you've just saved. loading the .MAT file may take time depending on the size or number of variables.
3 Comments
Jan
on 17 Apr 2016
@braa: It is the purpose of function to have their own workspace. Poluting a functions workspace with a bunch of variables created anywhere else is a really bad idea, because this impedes debugging seriously. There is no chance to decide, if the name of a variable contains a typo or not.
Matlab does not support an automatic import of a workspace, because this would be an ugly programming style. It would be prone to bugs and extremely inefficient concerning run-time, because the JIT acceleration cannot handle dynamically created variables. In consequence, relying on clean programming techniques can accelerate the code drastically (I've seen a factor of 100) and allow to maintain the code. So better hate ugly programming methods instead of Matlab.
The above "Answer", with some tweaks:
"I really love that MATLAB keeps the function spaces separate, which of course they should be! For me, I take advantage of this by encapsulating particular operations within a function, and keep its internal variables hidden. What happens inside a function is of no importance to the rest of my code, so it is great that MATLAB correctly defines each function with its own separate workspace. Then the variables in different workspaces don't affect each other and give unpredictable runtime errors. I would certainly never try to use some slow hack to pass all workspace variables uncontrollably: this would defeat the purpose of functions entirely! Passing variables correctly (or using nested functions) allows me to write fast, efficient programs, and to make debugging easy. I am glad that I understand how to write and use functions properly!"
Walter Roberson
on 7 Jan 2017
Ahmad kourani comments to Jan Simon:
This guy really knows what he is talking about!
Curtis Garner
on 16 Aug 2023
0 votes
One workaround that hasn't been mentioned so far is to replace the function with a script. In the main code, you replace
"[some results] = someFunction(<entire workspace>)"
with just
"someScript".
Then remove the function declaration line to turn the function into a script. There are definitely drawbacks and potential issues with this approach, but technically it provides the functionality the original poster was looking for, and in some cases it allows for much simpler code.
Categories
Find more on Performance and Memory 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!