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.
采纳的回答
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 个评论
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{:});
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 File Operations 的更多信息
另请参阅
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
