Re: Calling scripts from functions?
- From: Michael Wild <themiwi.REMOVE.THIS@xxxxxxxxxxxxxxx>
- Date: Thu, 09 Aug 2007 14:38:33 +0200
Karl Trumstedt wrote:
Michael Wild <themiwi.REMOVE.THIS@xxxxxxxxxxxxxxx> wrote in message <46bac60c$1@xxxxxxxxxxxxxxxxxxx>...Karl Trumstedt wrote:wrote inMichael Wild <themiwi.REMOVE.THIS@xxxxxxxxxxxxxxx>inmessage <46b9bea0$1@xxxxxxxxxxxxxxxxxxx>...Karl Trumstedt wrote:Hi,
I have a matlab script which puts a lot of variablesassignin,the(i.eworkspace.
I am calling this script when a user presses a buttonputfrom a function callback). Therefor all variables arein the callers workspace, not the base workspace.
Can I, in a simple way, put the variables in the base workspace? I know that I can use the commandright?butI need to do this every time I assign an variableonIs there an easier way to do it?help evalin
but this class of functions is bad and that you relyelementvariables in thebase workspace is even worse...Why is the class of functions bad? Guide generated them automatically so I would figure that it's a natural
michaelare notof Matlab.i'm talking about evalin/assigning/eval functions. those
generated by guide (at least not where one would see it).they breakscoping and can produce unpredictable results.access
Variables in the base workspace are just for havingtheto them at all times, so that you can access them fromare quite badcommand prompt at any time. Having the variables in the callers workspace will put them out of scope after the callback.that's true. but global scope (or base-workspace scope)
choices, just BECAUSE the user can easily change things.and there's theproblem that you might run into conflicts such as nameclashes. a smallchange in your code, like adding a call to a functionwhich overwritessome of the variables you depend upon without realizingit, and you'rescrewed.the handles
for the data exchange between callbacks you should usestructure maintained by the gui object. you can get andset it with theGUIDATA function. the guide-generated callbacks alreadyhave it as anargument for reading, but if you change it you still haveto store itback with GUIDATA. the handles structure is a datastructure containinghandles to the gui objects and their data. there's aspecial field,UserData, which is to be used by your code.
hth
michael
Hi,
thanks for your input.
I agree with what you say, the global scope generally is pretty bad, but I feel that Matlab handles its workspace differently then other languages.
yep, in the sense that it is "interactive" which makes it even worse for programs which rely on its contents. the base workspace is fine for ad-hoc command-line work and some mickey-mouse scripts, but not more.
Even if I would utilise the GUIDATA, the user would not be able to access the variables when the program has ended.
that's the point in the first place. if you want to give things back to the user, use the OutputFcn callback. it makes things explicit. after all, a user might become pretty pissed if your gui overwrote his results he obtained on the command line by accident. if you use OutputFcn this won't happen and your gui acts then as a normal function, taking some input and returning some output.
I think it's quite handy to have them there for the user to look at whenever needed and use in interactive plotting.
Of course, the ability to change the variables, that you mentioned, without knowing is a drawback.
How do you feel of the keyword global? That makes the variables global, but still only visible for the functions that "define" the global variable. I have been wondering if it is wise to use, both for regular variables and uicontrols. I have been using both the object handles and global variables to store controls and values.
i do not like it at all. for some very special cases it might be ok (say tic-toc), but not in general. if you think that your argument lists become too long, use structures to group things in a meaningful way. again, GLOBAL does not prevent any name-clashes. a new combination of functions (say your co-workers toolbox joined your code) might break things completely because you both use the same global variable.
By using global variables (with the keyword global) you still have to define which functions can alter the variable. It also keeps the handles structure a bit cleaner, since it's passed along to all callbacks (even when no controls are needed).
i wouldn't worry about the "cleanliness" of the handles structure if you have global variables floating around... true, name clashes could also happen in the handles structure, which is why i'd recommend to use structure the UserData field in the following way:
handles
UserData
Component1
Field1
Field2
...
Component2
Field1
Field2
...
...
this way you and your coworker don't accidentally overwrite each others fields in UserData.
Would be nice with some input on these issues, since I am mostly self-tought.
there are some very good tutorials on the web about gui programming with matlab. i'd especially recommend having look at http://blogs.mathworks.com/pick/2005/05/18/a-new-way-to-build-guis/ which gives you hints on how to do things without guide in a more efficient and simpler way.
michael
.
- Follow-Ups:
- Re: Calling scripts from functions?
- From: Karl Trumstedt
- Re: Calling scripts from functions?
- References:
- Calling scripts from functions?
- From: Karl Trumstedt
- Re: Calling scripts from functions?
- From: Michael Wild
- Re: Calling scripts from functions?
- From: Karl Trumstedt
- Re: Calling scripts from functions?
- From: Michael Wild
- Re: Calling scripts from functions?
- From: Karl Trumstedt
- Calling scripts from functions?
- Prev by Date: Re: GUIDE update after adding another component
- Next by Date: Re: Off-screen rendering
- Previous by thread: Re: Calling scripts from functions?
- Next by thread: Re: Calling scripts from functions?
- Index(es):
Relevant Pages
|