GUIDE, calling functions

1 view (last 30 days)
Johan
Johan on 26 May 2011
Hi all, might just be me, but I am having a hard time finding som info on GUIDE that I can use, I know nothing of GUIDE, but I know how to use and do code in matlab, but my problem is how to make a GUIDE that executes a function which lies in a seperate m-file when pushing a pushbutton
and also how to run a function with parameters, when i have two textboxes and a button, still running a function in a seperate m-file.
Is this even possible? searched google and mathworks without getting a usable answer.
  1 Comment
Johan
Johan on 26 May 2011
I really need some concrete examples, because all those "ressources" out there doesn't explain it for me.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 26 May 2011
  2 Comments
Johan
Johan on 26 May 2011
yeah I looked through those, not quite helping me, but maybe I just need some more time with GUI to get the hang of it, but thanks though.
Walter Roberson
Walter Roberson on 26 May 2011
Set the CallBack property of the button to
{@(src, event, handles) YourFunctionName(Parameter1, Parameter2, ....)}
I am not completely sure if handles will be passed by default.

Sign in to comment.

More Answers (8)

Ivan van der Kroon
Ivan van der Kroon on 26 May 2011
A gui is just some visualization of some m-file. If you want to call a separate m-file you have to call it in the first one.
If you want to call this functions with parameters you have to set them in the m-file as well, probably by adding some slider in your gui and then call the m-file with these variables as input.
Remember that a gui is just an interface and the execution is in the m-file.
  1 Comment
Johan
Johan on 26 May 2011
Sorry, but I have no idea what you are talking about...
I have about 10 m-files containing different functions, some are called from command window using only function name, others are called using function name and parameters in () what I need is a gui that "contains" all functions, so that the user can call them without using command window.
I now it's a pain, but please, could you direct me to some specific example, because I don't know what you mean when you say 'slider'.
And when you say 'If you want to call a separate m-file you have to call it in the first one.' How do I do that? I can call functions from other functions normally, but how to do it only when the pushbutton is pushed?

Sign in to comment.


Ivan van der Kroon
Ivan van der Kroon on 26 May 2011
When you are using the guide it is actually a gui for making gui's. What you are actually doing is making an m-file and this m-file makes a figure with some buttons, bullets and values. These are the callbacks and they are nothing else than changing values in the m-file's workspace.
For instance type
xpsound
and you see Matlab's sound capability gui. If you would instead type
edit xpsound
you get to see the m-file which generates this figure and its callbacks. In this case the volume bar is an example of a slider.
It appears that you only know how to make gui's with guide. Is that correct? I never made one with guide, but there should be a way to call other functions. If you cannot find it, I would suggest to add some arbitrary callback, save your gui and then change it in the m-file to execute another m-file of your choosing.
  1 Comment
Johan
Johan on 26 May 2011
First, look at the post below, second, I only tried with GUIDE yes, with no luck, I have absolutely no idea how to code GUI's, and it bugs me, I really want to learn it, but what you are saying is simply to general, I need something concrete, a specific example that I can learn from, if it exist, while looking here for answers I am also searching google and matlab help, and finding nothing that "teaches" me this.

Sign in to comment.


Johan
Johan on 26 May 2011
To specify:
In VBA (which I know a lot better than matlab coding) you can use: sub pushbutton_Click() Call FunctionName end sub
and then it runs that function, in matlab when calling from one function to another you just write the functionname, this I know, but how to code it so it runs the function when the button is pushed in gui?
  2 Comments
Ivan van der Kroon
Ivan van der Kroon on 26 May 2011
I'm sorry, but I'm not familiar with vba. There are a lot of tutorials in matlab to learn how to make gui's.
So, did you look into xpsound? The simplest action is the info-button. On line 330 it is checked for clicks and on line 331 it executes a simple command if it is clicked. Now just change this command with the m-file you want to call. You also mentioned assigning variables to the functions and lines 316-327 are examples of that.
Johan
Johan on 26 May 2011
but I don't need sound? I just need pushbuttons and textboxes, so I don't understand?
but I'll look.

Sign in to comment.


Johan
Johan on 26 May 2011
Okay, now I see it...somewhat...
though it's a bit complicated to adjust it to my needs.
but I tried, and when I run it it seems to be stuck in the initialize fase of the code....
is there any other good examples other than xpsound, I tried looking, but I just can seem to find anything usefull

Johan
Johan on 26 May 2011
Well played around with GUIDE some more, and actually managed to make it work, that is, when I run the m-file og .fig the commandwindow prints 10-15 errors, then in the figure I "initialize" my data from excel by calling an m-file which clears the command window, and after that there are no errors when doing the functions...
but still, annoying with all the errors, if someone can give som tips to clearing out any of the following errors it would be nice, I will try my self, but could be many of you know things about this I don't
??? Error using ==> feval
Undefined function or method 'edit4_CreateFcn' for input arguments of type 'double'.
Error in ==> gui_mainfcn at 96
feval(varargin{:});
Error in ==> starter at 42
gui_mainfcn(gui_State, varargin{:});
Error in ==>
@(hObject,eventdata)starter('edit4_CreateFcn',hObject,eventdata,guidata(hObject))
??? Error using ==> struct2handle
Error while evaluating uicontrol CreateFcn
??? Error using ==> feval
Undefined function or method 'edit12_CreateFcn' for input arguments of type 'double'.
Error in ==> gui_mainfcn at 96
feval(varargin{:});
Error in ==> starter at 42
gui_mainfcn(gui_State, varargin{:});
Error in ==>
@(hObject,eventdata)starter('edit12_CreateFcn',hObject,eventdata,guidata(hObject))
??? Error using ==> struct2handle
Error while evaluating uicontrol CreateFcn
??? Error using ==> feval
Undefined function or method 'edit9_CreateFcn' for input arguments of type 'double'.
Error in ==> gui_mainfcn at 96
feval(varargin{:});
Error in ==> starter at 42
gui_mainfcn(gui_State, varargin{:});
Error in ==>
@(hObject,eventdata)starter('edit9_CreateFcn',hObject,eventdata,guidata(hObject))
??? Error using ==> struct2handle
Error while evaluating uicontrol CreateFcn
??? Error using ==> feval
Undefined function or method 'edit4_CreateFcn' for input arguments of type 'double'.
Error in ==> gui_mainfcn at 96
feval(varargin{:});
Error in ==> starter at 42
gui_mainfcn(gui_State, varargin{:});
Error in ==>
@(hObject,eventdata)starter('edit4_CreateFcn',hObject,eventdata,guidata(hObject))
??? Error using ==> struct2handle
Error while evaluating uicontrol CreateFcn
??? Error using ==> feval
Undefined function or method 'edit3_CreateFcn' for input arguments of type 'double'.
Error in ==> gui_mainfcn at 96
feval(varargin{:});
Error in ==> starter at 42
gui_mainfcn(gui_State, varargin{:});
Error in ==>
@(hObject,eventdata)starter('edit3_CreateFcn',hObject,eventdata,guidata(hObject))
??? Error using ==> struct2handle
Error while evaluating uicontrol CreateFcn
  3 Comments
Walter Roberson
Walter Roberson on 26 May 2011
They are in the properties of the figure.
You either deleted something or renamed something.
Johan
Johan on 26 May 2011
not really no...I didn't touch that portion of the m-file, I only added things to the callbacks for my buttons and so on, and after making the m-file i went back to the figure and added some more controls, maybe that's it? or?

Sign in to comment.


Johan
Johan on 26 May 2011
Now I tried around a bit, even tried making the code from scratch, that is deleting the m-file, opening the fig with GUIDE, then saving, and then adding my callbacks...still getting a lot of errors.

Johan
Johan on 26 May 2011
Oh sweet...works quite good now...no errors, what I ended up doing was deleting the m-file, making a new GUI, copy pasting all the contents of my old fig, renaming all the controls to match the code I had before, and then save the fig, and create the new m-file, and copy the old code back in to the m-file, and then, no errors.
Now I have two questions remaining, I am calling a function that plots a couple of lines, when run from command window it pops up, but if I run it from GUI, it comes in the GUI itself, how to make it pop-up?
and the second I have an initializing function that needs to be run at the begining, how can I print out in the gui that it has been loaded?
And again thanks for all the help so far.
  3 Comments
Ivan van der Kroon
Ivan van der Kroon on 27 May 2011
I suppose you want to implement the correct figure handles?
Johan
Johan on 27 May 2011
that might be it? I don't know much about handles, I am learning by doing, any example code or something?

Sign in to comment.


Johan
Johan on 27 May 2011
did It myself...figured out how to make my function come into a new window...it was so simple...all I needed was to write the word 'figure' on the line before I ran my function...no need to do handles and stuff...
  1 Comment
Ivan van der Kroon
Ivan van der Kroon on 30 May 2011
Well, your doing handles when using figure! Nice to get it working!

Sign in to comment.

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!