how do I get the value of a variable from the base workspace in my GUI??

151 次查看(过去 30 天)
Hi,
i have a large workspace (±400 variables) and i want to write a script for my gui, where i can put in a variable in an edit box, press a button next to it, and then the value of this variable has to appear in an other edit box. I'm really stuck on this, does anybody know how to do this???
Greetings
  2 个评论
Edward
Edward 2012-10-25
one edit box to for the name of a variable, and one edit box where the value corresponding to this variable appears. Also there is a button, which has to run the script that gets de data from the workspace an shows it in the second edit box.

请先登录,再进行评论。

采纳的回答

Milos
Milos 2012-10-25
You can use this:
varName = evalin('base', 'varNameInWorkspace');
  11 个评论
Rik
Rik 2022-4-29
Sound like you need to load to a struct. That way you can use the fieldnames function to loop through all variables to store them in the appropriate array.
So again, no need for eval. That is the point. You should not assume eval is the answer. I haven't really seen a situation where there wasn't a solution. Well, one only: defining an anonymous function in an m file will cause a syntax error in Matlab 6.5, because that isn't a valid syntax. And maybe there is a workaround even there.
If you have a problem, explain the problem. The issue is that many people end the explanation of their problem with 'how can I use eval to do this'.
Stephen23
Stephen23 2022-4-29
编辑:Stephen23 2022-4-29
@Mohammad Shokrian Amiri: It is curious that you think "that is in your mind counter-productive and bad coding habit": the fact that this approach leads to slow, complex, inefficient code that is harder to debug is objective reality that has nothing to do with my poor little mind. The fact that the MATLAB documentation states here "Best Practice: Passing Arguments" and places evaluating variables in another workspace at the end of the list... also has nothing to do with my mind. You can confirm those facts yourself.
I completely agree that simply writing "do not do it" is not very useful. That is exactly why:
  • I started this thread which contains explanations/links of the main disadvantages and also explanations/links to the better (faster, neater, simpler, more robust) alternatives. So any user who actually wants to learn has the opportunity to do so.
  • on many threads where the OP/commenter has given sufficient information I also write a comment/answer that shows a more efficient and more robust approach, with code and links to help them. Feel free to read my many comments and answers on this topic to confirm that yourself.
"...I am struggling with a PROBLEM!"
You will also find several threads on this forum discussing the meta-topic of whether the active users on this forum should only answer the exact question that the OP asked (even if this leads to them wasting significant amounts of their own time and writing pointlessly complex and inefficient code) vs. giving advice based on our combined thousands-of-hours and millions-of-lines of experience. Which would you prefer? And if the latter, how should it be phrased to effectively communicate its content?
As Rik already commented, most likely your code would be improved by LOADing into an output variable.

请先登录,再进行评论。

更多回答(0 个)

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by