Is it possible to pass all variables of current workspace into a function without using 'load'

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?

1 Comment

****************(I comment this for myself)*********
%%%%%%%Walter's 2 comments on Jan's answer is very important%%%%%%%%%%%%

Sign in to comment.

 Accepted Answer

Based on your previous question, I sense you are going towards a direction which may not be the best approach.
You've been able to read all those 600 .xls files relatively fast using COM server. Assume memory is not an issue, you can read in all the data at once.
If you need to use the data inside a function, then run that COM server code inside the function so you get the data right inside the function workspace.
If you also need to use the data somewhere else, or multiple functions, then make a function to read all the .xls file and put the result data in the return argument. Something like this, function Data=ReadXlsFile(Folder). Then you can pass the Data around.
Regarding 600 variable names, you can definitely avoid that. Use XlsFiles=dir(fullfile(Folder,'*.xls')) will give you a structure array containing, for example, 600 .xls file names.
Then you can use the function to read in all the data and put them in a cell array Data=cell(600,1). Data{1} will contain the data for file XlsFiles(1).name, Data{2} will contain the data for file XlsFiles(2).name. Everything is well organized. All you need is an index, k=1, or k=20, or k=600, you can find the file name, which is XlsFile(k).name, or the data, which is Data{k}.

4 Comments

so much thanks dear Fangjun
you explain really very very nice
i am using the COM sever code that you told and it's a very nice code but these 600 .xls file in one of 10 operation (i told in above comment)must be compared with together that it means 600*600=360,000 times comparing and 360,000 reading with that COM server code instead of reading 600 times if i put them in workspace
"If you also need to use the data somewhere else, or multiple functions, then make a function to read all the .xls file and put the result data in the return argument"
@Fangjun I don't need result of them in each operation, it's needed themselves (data of each .xls is needed not other thing)
if you recommend this, I'll do this.
I am still interest in that, is there any way for sharing base workspace with an specified function?
Maybe declare the variable as global, then it can be used anywhere. But it is not a recommended practice. doc global to find out more.
Thanks a lot
Here is really a nice where for giving help
I specially appreciate Fangjun Jiang, Jan Simon, Walter Roberson, Daniel,Paulo Silva, Grzegorz Knor, and other persons that i can't remember their names
****************(I comment this for myself)*********
%%%%%%%Walter's comment on Jan's answer is very important%%%%%%%%%%%%

Sign in to comment.

More Answers (4)

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

Jan thanks for replying
reason of using 600 variables in workspace:there are more than 600 .xls files. its needed to plot 2 .xls files of them together (subplot). First i made a cell that arrays of this cell are the names of all 600 .xls files. so i made this arrays, hyperlink to link each array to a function for plotting related two xls files (these two .xls file is related to the array that i clicked on it) (and @Jan you have helped me to do this here :
http://www.mathworks.com/matlabcentral/answers/16074-is-it-possible-to-make-command-window-scroll-bar-being-in-position-of-it-self-even-by-runnig-a-m-fi
and please see here too:
http://www.mathworks.com/matlabcentral/answers/15946-is-it-possible-in-matlab-to-make-arrays-of-a-cell-matrix-linkable-to-a-special-path-linkable-with )
because of this its needed to import all of 600 .xls files to workspace
and now it reads each .xls file only for one time and when .mat of that .xls comes on workspace makeing other analyzing on this .xls file more speedy and so not needs reading again this .xls file
You do not need one variable for each xls file: use a cell array instead.
@Walter it's needed doing variety operation(for example 10 operation) on each .xls file. if for each operation, use xlsread() command then for 10 operation, it use 10*600 times xlsread command. and this command take about 1 second (in my system). it means 6000 seconds must take to get result! but when i put them on workspace only one time these files be read (600 times using of xlsread) and for ever operation it doesn't need to read again.
Walter please explain more about how using of cell array.
http://matlab.wikia.com/wiki/FAQ#How_can_I_create_variables_A1.2C_A2.2C....2CA10_in_a_loop.3F
Thanks Walter but my problem doesn't relate to that link!
it only a method for naming in a1, a2, a3,... ! and i used it in my project
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.
Oh, really nice, now I understand what you mean
perfect!
thanks a lot
@Walter
and could i use cell2mat(A{i}) for convert it to data matrix?
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.
great!
some are 10000*2 and some are 10001*2
I could eliminate of 'end row' of 10001*2 so please explain more about 3D array (really i am going to love MATLAB! it's really a nice software)
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.

Sign in to comment.

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

Thanks a lot
Please refer me some references for studying about structure, i am beginner in matlam
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).

Sign in to comment.

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

@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!"
Ahmad kourani comments to Jan Simon:
This guy really knows what he is talking about!

Sign in to comment.

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

Community Treasure Hunt

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

Start Hunting!