Opening a file that requires another file for input

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

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

I put a comment on both posts in case it helps either way. This looks like it could work, though, as GUIDE already puts in a function listbox1_callback code, where does this fit in? Also, does this use the pushbutton to run the file 'Run_Program'?
Thanks
substitute the GUIDE listbox1_callback code by Walter's code, the program runs when you select the listbox value.
Ok, got that substituted; now I am getting an error message Run_Program is not designed for cell inputs. Walters code uses 'cellstr', is there some way of using something to have the selected listbox file remain a string... if that makes any sense?
If it helps.. my code for the listbox looks like
D = dir('C:\testingdirectory\*.txt'); % Dir Name.
FileNameList = {D(:).name}; %Listing by names
set(hObject,'string',FileNameList);
Run_Program(char(thisfile));
Got it, thanks for all the help
Sorry, should have been
thisfile = filechoices{fileidx};

Sign in to comment.

More Answers (1)

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

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!