Re: Calling scripts from functions?



Karl Trumstedt wrote:
Michael Wild <themiwi.REMOVE.THIS@xxxxxxxxxxxxxxx> wrote in message <46bac60c$1@xxxxxxxxxxxxxxxxxxx>...
Karl Trumstedt wrote:
Michael Wild <themiwi.REMOVE.THIS@xxxxxxxxxxxxxxx>
wrote in
message <46b9bea0$1@xxxxxxxxxxxxxxxxxxx>...
Karl Trumstedt wrote:
Hi,

I have a matlab script which puts a lot of variables
in
the
workspace.

I am calling this script when a user presses a button
(i.e
from a function callback). Therefor all variables are
put
in 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 command
assignin,
but
I need to do this every time I assign an variable
right?
Is there an easier way to do it?
help evalin

but this class of functions is bad and that you rely
on
variables in the
base workspace is even worse...

michael
Why is the class of functions bad? Guide generated them automatically so I would figure that it's a natural
element
of Matlab.

i'm talking about evalin/assigning/eval functions. those
are not
generated by guide (at least not where one would see it).
they break
scoping and can produce unpredictable results.

Variables in the base workspace are just for having
access
to them at all times, so that you can access them from
the
command 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)
are quite bad
choices, just BECAUSE the user can easily change things.
and there's the
problem that you might run into conflicts such as name
clashes. a small
change in your code, like adding a call to a function
which overwrites
some of the variables you depend upon without realizing
it, and you're
screwed.

for the data exchange between callbacks you should use
the handles
structure maintained by the gui object. you can get and
set it with the
GUIDATA function. the guide-generated callbacks already
have it as an
argument for reading, but if you change it you still have
to store it
back with GUIDATA. the handles structure is a data
structure containing
handles to the gui objects and their data. there's a
special 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
.



Relevant Pages

  • Re: Doesnt pass the value
    ... In the other callback ... I am not sure weather your code will work fine in the first call back itself, ... In matlab v7.1 to transfer the value of a variable from a function to another function in the same script I was doing it with ... but in matlab v7.7 doesn't work. ...
    (comp.soft-sys.matlab)
  • Re: Does MatLab cache files pr. instance
    ... an .m file that I include in the script that is run. ... If I start another matlab on the same computer - while the first one ... working in a single session and using the Matlab editor then Matlab ... the clear command must contain the entire ...
    (comp.soft-sys.matlab)
  • Re: Scripts with functions?
    ... Put them in the directory and go on; how much time can that take when as you say you're spending the major fraction of time in writing/debugging/using the script m-file... ... Or use Octave if it indeed is so much more productive owing to this feature while/until TMW acts on your enhancement request. ... Just out of curiosity, though, as I've never used Octave to speak of and never the specific way you're speaking of (I'd not have thought of it given that Matlab doesn't do that and Octave is, I thought, an open-source Matlab workalike so I'd have expected the same behavior), does Octave expose these functions in these script files to other than the calling script file in which they are contained? ... To paraphrase others in another language group I frequent when folks complain that the particular language of the subject group doesn't do something as another language does, ...
    (comp.soft-sys.matlab)
  • OS/X - "unexpected MATLAB operator" when run from command line
    ... I want to run a MATLAB script as a cron job on a machine running OS/X. ... saveas(gcf, fullfile(out_dir, png_file)); ...
    (comp.soft-sys.matlab)
  • Re: Java drag & drop
    ... I'm afraid the acceptDrop() hunch is incorrect, since if you call dtde.acceptDropin the Matlab callback it still does not prevent an InvalidDnDOperationException being thrown from dtde.getTransferable. ... The basic idea is to use a list of plot command strings as drag source, and drop its items onto Matlab axes that will plot the requested command. ...
    (comp.soft-sys.matlab)