Opening a file that requires another file for input

3 views (last 30 days)
I have an m-file that requires a .txt file as input in order for the m-file to run.
Something like
Run_Program(FILENAME)
%Run_Program(FILENAME)runs the program based on input commands as
%specified in ./runprogram/FILENAME.
where FILENAME corresponds to the name of the .txt file I wish to use.
I have a GUI (using GUIDE) which contains a listbox and a pushbutton. The listbox contains the .txt files that I can choose from in order to run the m-file. Normally I would just enter the name of the m-file into the pushbutton code to run it, but since the m-file cannot run on its own, and needs the selected file from the listbox, what would the code look like? To try and clarify, I want to run the m-file Run_Program with whichever .txt file is selected from the listbox via the pushbutton

Accepted Answer

Walter Roberson
Walter Roberson on 2 Mar 2011
Was it this you were looking for?
function listbox1_callback(src, event, handles)
fileidx = get(src, 'Value');
if isempty(value); return; end
filechoices = cellstr(get(src, 'String'));
thisfile = filechoices(fileidx);
set(src, 'Enable', 'off');
Run_Program(thisfile);
set(src, 'Enable', 'on');
end
  6 Comments
Walter Roberson
Walter Roberson on 4 Mar 2011
Sorry, should have been
thisfile = filechoices{fileidx};

Sign in to comment.

More Answers (1)

Matt Fig
Matt Fig on 2 Mar 2011
Get the string in the listbox, and pass it to the function which needs it. In the callback for the pushbutton, you put something like this:
C = get(listboxhandle,{'string','value'}); % Use the handle to the listbox.
mfl = C{1}(C{2}); % The M-File string name.
Run_Program(mfl) % Run the M-File
.
.
.
.
.
.
EDIT
In response to your comment, here is an example on one of my GUIDE GUIS. To figure out what GUIDE called the uicontrols, do this at the command line:
>> H = LISTBOX_EX % Call the GUIDE GUI, return handle.
H =
158.002563476563
>> guidata(H) % This shows the names of the uicontrols.
ans =
figure1: 158.002563476563
listbox1: 0.002685546875
pushbutton1: 159.002563476563
output: 158.002563476563
Now that you know the name of the listbox, in the callback to the pushbutton put this:
H = guidata(gcbo);
C = get(H.listbox1,{'string','value'}); % Use the handle to the listbox.
mfl = C{1}(C{2}); % The M-File string name.
Run_Program(mfl) % Run the M-File
  1 Comment
Dave
Dave on 3 Mar 2011
I put a comment on both posts, in case it helps either way. For your code, I'm not sure what to put for listboxhandle. As GUIDE put the GUI together, I don't know what the listbox is called. I tried entering the tag that it was named, but I continually get undefined function or variable.
Thanks

Sign in to comment.

Categories

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

Community Treasure Hunt

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

Start Hunting!