My Matlab.m file can't find variabels in the base workspace. How can I solve this.
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hello
I've wrote a program as following: -I declare a lot of variabels in an matlab.m file. -Once they are all declared an interface.mlapp file (an application created in app designer) opens and with an nice interface I can change the variabels. -If i click 'OK' in the interface an seconde matlab.m files opens and does calculations with the variabels in the workspace.
The problem is that my seconde matlab.m files doesn't find variabels WHICH ARE in the base workspace. Do I have to move all my variabels into an function workspace? and how do I do that? It are a lot of variabels...
I look forward to your answers.. Daan Decleer
3 Commenti
Stephen23
il 25 Apr 2017
Modificato: Stephen23
il 25 Apr 2017
"My Matlab.m file can't find variabels in the base workspace. How can I solve this."
There is nothing to solve because a function workspace should be separate from the base workspace. That is the whole point of them! Think about it: do you know what happens inside of sin? Do you know what internal variables it uses? Would you want the output of sin to change depending on what was in the base workspace? This is the point of functions: their workspaces are independent. And this is how they should be.
Instead: convert all of your scripts into functions. Do NOT put work variables into the base workspace. Scripts are great for playing around with some code, but if you want to do reliable code and reproducible code outputs then you need to use functions.
Move data between workspaces reliably and simply by passing them as arguments (perhaps in struct), exactly as the MATLAB documentation recommends:
Do not use globals, assignin, or evalin or any other slow, buggy hack code.
Risposte (2)
Image Analyst
il 25 Apr 2017
I agree with Adam. Make the first m-file script into a function, and put the values into a struct and pass the struct out to your second m-file which will call your first m-file. Passing variables between different workspaces is kludgy and complicated (using things like setappdata, evalin, assignin, etc.) whereas this is precisely what functions are made for.
Further info is in the FAQ: http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.28s.29.3F
0 Commenti
Vedere anche
Categorie
Scopri di più su Matrix Indexing 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!