How do I ask for a workspace variable input in a running program?
17 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Aaron Greenbaum
il 16 Giu 2016
Modificato: Aaron Greenbaum
il 22 Giu 2016
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?
0 Commenti
Risposta accettata
John D'Errico
il 16 Giu 2016
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 Commento
Star Strider
il 17 Giu 2016
+1 John!
Leave it to you to have already solved this problem — 4 years ago!
Più risposte (3)
Stephen23
il 16 Giu 2016
Modificato: Stephen23
il 16 Giu 2016
>> 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 Commenti
Stephen23
il 17 Giu 2016
Modificato: Stephen23
il 17 Giu 2016
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:
- the functions returns a matrix, which are allocated to variables.
- 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
il 16 Giu 2016
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 Commenti
Vedere anche
Categorie
Scopri di più su Whos in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!