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

119 次查看(过去 30 天)
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?

采纳的回答

Fangjun Jiang
Fangjun Jiang 2011-9-23
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 个评论
mohammad
mohammad 2011-9-23
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
mohammad
mohammad 2011-9-23
****************(I comment this for myself)*********
%%%%%%%Walter's comment on Jan's answer is very important%%%%%%%%%%%%

请先登录,再进行评论。

更多回答(4 个)

Jan
Jan 2011-9-23
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 个评论
Walter Roberson
Walter Roberson 2011-9-23
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.

请先登录,再进行评论。


Daniel Shub
Daniel Shub 2011-9-23
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 个评论
Daniel Shub
Daniel Shub 2011-9-23
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).

请先登录,再进行评论。


braa
braa 2016-4-16
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 个评论
Stephen23
Stephen23 2016-4-17
编辑:Stephen23 2016-4-18
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!"

请先登录,再进行评论。


Curtis Garner
Curtis Garner 2023-8-16
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.

类别

Help CenterFile Exchange 中查找有关 Variables 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by