GUI Pushbutton, accessing another file

Hello,
I am fairly new to matlab, and attempting to make a basic GUI that has two pushbuttons, along with some other things. My question relates to the pushbuttons. I am wondering how to write the code so that when the user clicks a pushbutton, it opens up another file.
So, matlab automatically brings up:
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
and I am wondering where I put in code to allow the pushbutton, when clicked, to open up my other m file?
Cheers~

 Accepted Answer

Do you mean that your file is hardcoded into the GUI? If so, put this in the pushbutton callback:
edit mfilename
where you replace mfilename with the name of your M-File.
If the file is not in the current directory, you will have to add the path.
If that is not what you meant, you might want to use UIGETFILE instead:
[fnam,pth] = uigetfile('*m')
edit([pth fnam])

6 Comments

Both the GUI file and the m file I want to bring up when the pushbutton is clicked are in the same directory. My question is with the coding, when matlab sets up a GUI the above posted code is given, I am wondering what code to put in and where to bring up my other m file when the button is clicked.
Put the code I gave you (the first one) in the callback for the pushbutton. That means: below the last line of the code you showed. Just try it first then let us know what happened.
I did what you said, and it brought up the m-file editor for the specified m-file. I got rid of 'edit' and just put the name of the m-file below the line of code I posted, and it did what I wanted it to; when the pushbutton was clicked, it opened my other m-file. The problem now, is that after I click that button, the GUI closes and the other file opens. Is there a way to have the other file open without the original GUI closing?
The file that the GUI opens up via the push button uses the Command Window and requires user input. Not sure if that is relevant to the GUI closing or not, but figured I'd let you know in case that's the culprit.
Oh, I see your terminology is what was confusing about your post. You wanted, not to OPEN the M-File, but to RUN the M-File. Right? Is there a line in the M-File, like:
clear all,close all,clc
or similar? Specifically, any calls to the CLOSE function?
Ah, sorry for the confusion, my mistake; running the file is what I wanted.
Yes, I just looked, and there is a command for close all. Got rid of that, and it works quite well. Thank you so much for the help, much appreciated.
I do have another question, if you wouldn't mind. I have a folder of files I wish to display in a listbox, then have the user select a file from the box, then click another pushbutton to run the selected file. I was looking at http://www.mathworks.com/help/techdoc/creating_guis/f6-7446.html#f6-11263 but was confused where the path to the directory goes or how I add that.
Thanks again.
I recommend you select a best answer for this thread, then start a new one because this is a new topic.

Sign in to comment.

More Answers (1)

fig=figure
uicontrol('Style','pushbutton','String','Start',...
'Callback','SomeMFileNameIWantToOpen',...
'Units','Normalized','Position',[0.5 0.5 0.1 0.1],...
'Parent',fig)

2 Comments

He is using GUIDE...
And this is not advisable if there could be more than that simple call to run the other M-File in the callback.
It is so helpful. You saved my day ^_^

Sign in to comment.

Categories

Asked:

on 23 Feb 2011

Community Treasure Hunt

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

Start Hunting!