How do I ask for a workspace variable input in a running program?

I'd like to be able to set a variable in a code to a workspace variable that the user can choose.
Assume I have some variable B in the workspace A=input('Please pick the variable you would like to use... ');
How do I set A equal to B while the program is running?

 採用された回答

John D'Errico
John D'Errico 2016 年 6 月 16 日

2 投票

A simple answer is uigetvar , posted on the file exchange.
It pops up a dialog box, listing the names of all variables in your base workspace. You select the desired variable, and it returns the contents of that variable to your function of script.

1 件のコメント

Star Strider
Star Strider 2016 年 6 月 17 日
+1 John!
Leave it to you to have already solved this problem — 4 years ago!

サインインしてコメントする。

その他の回答 (3 件)

Stephen23
Stephen23 2016 年 6 月 16 日
編集済み: Stephen23 2016 年 6 月 16 日

1 投票

Put all of your variables into a structure. Then you can simply do this:
>> S.a = 1;
>> S.b = 2;
>> S.c = 99;
>> A = input('input variable: ','s');
input variable: c
>> S.(A)
ans =
99
Solutions to problems can be so simple, when you are prepared to think a little outside the box.

4 件のコメント

Aaron Greenbaum
Aaron Greenbaum 2016 年 6 月 16 日
Thanks this is definitely an option. Do you know if there is any other ways to do this? This would require the user to rename variables before they used them. There must be some way to be able to call variables as input right?
Stephen23
Stephen23 2016 年 6 月 16 日
編集済み: Stephen23 2016 年 6 月 16 日
  1. there are ways of manipulating arbitrary variables, but they are slow, and will cause more bugs than you can imagine... bugs that are very difficult to debug. And beginners love this option!!
  2. The idea of manipulating arbitrary variables in the base workspace... it sounds like a bad idea. What about wrapping your operations in functions, to keep them independent?
  3. Don't try to create an entire parser using MATLAB. Although beginners often want that "the user can input any variable, and pick any function...", this is inefficient and slow. Think about your task: what are you actually trying to achieve ?
Give us a bit more info about your task and we can help find a neat and efficient way to code it.
Aaron Greenbaum
Aaron Greenbaum 2016 年 6 月 16 日
I made a function with no arguments to browse for an hdf file on the computer and extract an image in the form of an 1024x1024 matrix the function finds the peak locations in the image. I have another script that takes the matrix and warps it and exports a new matrix. I would like to be able to use one of these new matrices in the peakfinding function.
I am pretty sure that I could make this work by adding an argument to my function and having a default if no argument is input, but I was wondering if there were otherways to do this. (Without using eval)
[centroids] = PeakFind(imagevarname)
if nargin==0
browse
else
img1=imagevarname;
end
Stephen23
Stephen23 2016 年 6 月 17 日
編集済み: Stephen23 2016 年 6 月 17 日
The core question is: how do you get lots of matrices into your workspace?
This might happen is several ways, but lets consider two common ways:
  1. the functions returns a matrix, which are allocated to variables.
  2. the matrices are magically "poofed" into existence in the base workspace using assignin.
In the the first case the user allocates this matrix to a variable, so equally they can supply this variable as an input argument when calling the second function. In the second case, a poor program design makes the rest of the code more difficult... Unfortunately we don't have enough information on how you are doing these steps.
Some general advice though: remember that the KISS principal applies to writing code! If the task is basically to pass data from one function to another, then pass them simply by using the input/output arguments (which is fast and reliable). Concentrate your activities on actually solving your tasks, not on trying to replicate a functionality that already essentially exists (passing variables from one function to another).
For keeping track of lots of variables, you can put them all into one matrix, one cell array, one structure, etc. This is the simplest way to store them because you can trivially loop over that data. There are also many MATLAB operations that operate on entire matrices/cell arrays/structures all at once, which makes working on the data much faster and neater than if you try to access separate variables.
A numeric array is simplest, a structure lets you use fieldnames...
A little thinking, experimenting, and planning of how to achieve your task goes a long way:

サインインしてコメントする。

Star Strider
Star Strider 2016 年 6 月 16 日

1 投票

If you have a list of variables already in your workspace, and you want the user to choose one, I would use the listdlg function.
It is much easier than using input and the associated logic involved in selecting a variable.

8 件のコメント

Stephen23
Stephen23 2016 年 6 月 16 日
編集済み: Stephen23 2016 年 6 月 16 日
That does not answer how to dynamically make A equal to B...
Star Strider
Star Strider 2016 年 6 月 16 日
It would be relatively easy to do that with listdlg. The whos function could be used to create the list of workspace variables for listdlg.
We don’t know all the details, so writing exact code is not currently possible.
Aaron Greenbaum
Aaron Greenbaum 2016 年 6 月 16 日
I think is is probably what I am looking for the only problem is that who makes a cell with the names of variables as strings, not variables. I've tried this basic code and found that it returns the variable name as a string, not the data in the variable.
workspacevars=who;
disp('Please indicate which image data to use');
varnum= listdlg('ListString',workspacevars);
img1=workspacevars{varnum};
Do you have any advice?
Star Strider
Star Strider 2016 年 6 月 16 日
The easiest option might be to use ‘the evil eval’, although I believe it is unfairly demonised. It definitely has its uses:
workspacevars=who;
disp('Please indicate which image data to use');
varnum= listdlg('ListString',workspacevars);
img1=workspacevars{varnum};
var = eval(img1)
This will return the value of the chosen variable in the ‘var’ variable assignment. (Name it something that makes sense in the context of your code and application.)
Stephen23
Stephen23 2016 年 6 月 16 日
編集済み: Stephen23 2016 年 6 月 16 日
Of course the "evil eval" could be easily avoided by simply putting all of the data into one variable in the first place: a structure, a cell array, a matrix,... instead of into multiple variables. Fix the problem at the source by more robust program/data design, rather than trying to fix it later by using a tool that is slow and almost impossible to debug.
"it is unfairly demonised" no demons, just cryptic and hard to debug bugs...
Star Strider
Star Strider 2016 年 6 月 16 日
... and occasionally necessary.
Stephen23
Stephen23 2016 年 6 月 16 日
... to fix a beginner's poor program design.
Star Strider
Star Strider 2016 年 6 月 16 日
Your words, not mine.

サインインしてコメントする。

Aaron Greenbaum
Aaron Greenbaum 2016 年 6 月 22 日
編集済み: Aaron Greenbaum 2016 年 6 月 22 日

1 投票

You can use John's Uigetvar and putvar to do this, but it does use the dreaded Eval function (evalin). Overall it is better to improve the way you structure your code such as putting your variables into a structure which can then be called for the required variable. In the end I ended up using nargin to have a more flexible function that I could use with or without an input image. Thanks everyone for your help!

カテゴリ

ヘルプ センター および File ExchangeFunction Creation についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by