Displaying Files using a Listbox in a GUIDE GUI.

I have written a GUI that pulls data from a file and makes plots. Currently, I have coded a pushbutton to retrieve the files, but I want to put a listbox in the GUI that acts as a directory.
I figure this is a common operation, but I haven't been able to find much to get me started.

 Accepted Answer

Where exactly are you running into trouble? Posting your relevant code so far might help. As for displaying the files in the listbox, to do that you just need to set the box's 'String' property.
d = dir; %get files
set(handles.my_listbox,'String',{d.name}) %set string

8 Comments

I don't know what code would be relevant. But what I am trying is the following code in my listbox CreateFcn:
D = dir('C:\Users\MATLAB\Data')
D = {D(:).name};
set(hObject, 'string', D);
This seems to be similar to what you're telling me to do, but I'm not getting anything to work.
I see that
d = dir;
gets me the directory that my GUI files are in. Can I have it go to a different folder or have it see only EXCEL files?
To have it go to a different directory, just do what you do in your above comment: pass a string representing the path to the folder you want to look in to dir.
If you wanted to find only filetypes with certain extensions, you could parse the returned strings and only keep those that contain the string '.xls' or '.xlsx'
D = dir('C:\Users\MATLAB\Data')
D = {D(:).name};
D = D(cellfun(@(x)~isempty(regexp(x,'.xls')),D))
Also, if you want to set the string to the listbox from within the callback to your pushbutton, you need to pass in the handle to the listbox into set instead of the argument hObject. hObject is the handle to the object whose callback is currently executing, so the pushbutton's callback will try to set the pushbutton's string to your file names instead of handles.my_listbox (where my_listbox is your listbox's Tag property).
Okay, I've almost got this all working. Thank you so much for your help. How would I get my "run" pushbutton to read what is in the listbox. Right now I have the run callback doing the following:
% Read EXCEL file highlighted in listbox
a = 'C:\Users\MATLAB\Data';
b = get(handles.listbox1, 'string');
filename = strcat(a,b);
%Read columns from Sheet1 of selected file into MATLAB
[data, text] = xlsread(filename, 'A:H');
I'm getting an error that "filename" must be a string for xlsread. But it should be a string already since "a" and "b" are strings. Do you see anything wrong with my code?
The problem is that the 'String' property for a listbox is always the entire contents of that listbox, even when you have a particular row selected. To isolate a single string (usually the selected one) from your entire cell array, you need to index it based on the 'Value' property of your listbox.
% Read EXCEL file highlighted in listbox
a = 'C:\Users\MATLAB\Data';
b = get(handles.listbox1, 'string');
v = get(handles.lixtbox1, 'value '); %get current selection value
b = b{v}; %choose corresponding cell
filename = strcat(a,b);
%Read columns from Sheet1 of selected file into MATLAB
[data, text] = xlsread(filename, 'A:H');
That did it. Much appreciated, Evan.
No problem! Glad you have it working.
not working
[data,text] = xlsread('DB.xls','Users','B3:B100') % Read EXCEL file highlighted in listbox a = 'C:\Users\user\Documents\LogIn\DB.xls'; b = get(handles.lbox, 'string'); v = get(handles.lbox, 'value '); %get current selection value b = b{v}; %choose corresponding cell DB.xls = strcat(a,b); %Read columns from Sheet1 of selected file into MATLAB
Error in editemployee (line 42) gui_mainfcn(gui_State, varargin{:});

Sign in to comment.

More Answers (0)

Categories

Asked:

on 12 Aug 2013

Commented:

on 7 Mar 2016

Community Treasure Hunt

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

Start Hunting!