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.
採用された回答
Evan
2013 年 8 月 12 日
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 件のコメント
Caleb
2013 年 8 月 12 日
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.
Caleb
2013 年 8 月 12 日
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).
Caleb
2013 年 8 月 12 日
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');
Caleb
2013 年 8 月 12 日
That did it. Much appreciated, Evan.
Evan
2013 年 8 月 12 日
No problem! Glad you have it working.
DJ Sabio
2016 年 3 月 7 日
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 についてさらに検索
参考
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
