下午好!
我目前正在尝试使用App Designer使用MATLAB构建应用程序。我们的目标是能够使用多个GPX文件绘制数据,而我已经成功地完成了这些工作。我很好奇我应该如何填充列表框。
是否有一种方法使Listbox填充文件名,具体取决于您选择的文件夹?
答案 0 :(得分:0)
您可以使用dir
列出文件夹的内容,将名称列表转换为单元格数组,并使用单元格数组填充ListBox Items。
假设您有一个带有ButtonButtonPushed
回调的Button,并且希望让用户选择一个文件夹,然后在列表框中填充所有* .gpx文件。
您可以按照以下步骤进行操作:
% Button button pushed function
function ButtonButtonPushed(app)
selpath = uigetdir(); %Open dialog box for selecting folder.
gpx_files = dir(fullfile(selpath, '*.gpx')); %Dir all *.gpx in selected folder.
%Populate listbox with file names:
app.ListBox.Items = {gpx_files(:).name};
end
语句app.ListBox.Items = {gpx_files(:).name};
填充列表框。
gpx_files(:).name
是文件名列表。 {gpx_files(:).name}
从列表中创建一个单元格数组。 app.ListBox.Items = {gpx_files(:).name};
使用创建的单元格数组设置ListBox的Items
属性。 获取所选文件的完整路径:
保留所选文件夹:
添加一个名为selpath
的私有属性(使用设计器[代码视图]中的红色P +添加新属性,然后编辑该属性的名称):
properties (Access = private)
selpath % Store selected path
end
在按下按钮时将所选路径存储在selpath
属性中:
% Button pushed function: Button
function ButtonButtonPushed(app, event)
app.selpath = uigetdir(); %Open dialog box for selecting folder.
gpx_files = dir(fullfile(app.selpath, '*.gpx')); %Dir all *.gpx in selected folder.
%Populate listbox with file names:
app.ListBox.Items = {gpx_files(:).name};
end
现在,所选路径存储在app.selpath
中。
添加“ ListBoxChangeValue”回调(在设计视图中右键单击列表框)。
编辑ListBoxValueChanged函数的代码:
返回的value
value = app.ListBox.Value;
是所选文件的名称
(您无需使用strcmpi
)。
使用fullfile
函数将路径与文件名连接起来。
% Value changed function: ListBox
function ListBoxValueChanged(app, event)
value = app.ListBox.Value;
selected_file = fullfile(app.selpath, value); %Get the full path of selected file.
disp(selected_file) %Change the code to load selected_file
end
上面的代码在命令窗口中显示selected_file
字符串。
将disp(selected_file)
替换为您自己的代码(加载并绘制gpx文件)。
这是App1的完整代码(大多数代码是自动生成的):
classdef App1 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
Button matlab.ui.control.Button
LabelListBox matlab.ui.control.Label
ListBox matlab.ui.control.ListBox
end
properties (Access = private)
selpath % Store selected path
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
end
% Button pushed function: Button
function ButtonButtonPushed(app, event)
app.selpath = uigetdir(); %Open dialog box for selecting folder.
gpx_files = dir(fullfile(app.selpath, '*.gpx')); %Dir all *.gpx in selected folder.
%Populate listbox with file names:
app.ListBox.Items = {gpx_files(:).name};
end
% Value changed function: ListBox
function ListBoxValueChanged(app, event)
value = app.ListBox.Value;
selected_file = fullfile(app.selpath, value); %Get the full path of selected file.
disp(selected_file) %Change the code to load selected_file
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [101 101 640 480];
app.UIFigure.Name = 'UI Figure';
% Create Button
app.Button = uibutton(app.UIFigure, 'push');
app.Button.ButtonPushedFcn = createCallbackFcn(app, @ButtonButtonPushed, true);
app.Button.Position = [43 380 114 49];
app.Button.Text = 'Select Folder';
% Create LabelListBox
app.LabelListBox = uilabel(app.UIFigure);
app.LabelListBox.HorizontalAlignment = 'right';
app.LabelListBox.VerticalAlignment = 'top';
app.LabelListBox.Position = [300 412 44 15];
app.LabelListBox.Text = 'List Box';
% Create ListBox
app.ListBox = uilistbox(app.UIFigure);
app.ListBox.ValueChangedFcn = createCallbackFcn(app, @ListBoxValueChanged, true);
app.ListBox.Position = [359 355 100 74];
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = App1
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
% Execute the startup function
runStartupFcn(app, @startupFcn)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end