在 MATLAB 中将数据与 ListBox 相关联

Associating Data with ListBox in MATLAB

计算器溢出!

我很高兴地说,我已经能够创建一个列表框,该列表框根据文件夹的 selection 填充文件名。但是,我 运行 遇到了很大的障碍。

我希望能够根据我的列表框 selection 绘制数据。目前我的 MATLAB 程序执行以下操作: 1. 用户可以使用 PshButton 功能 "Search" 找到所需的文件夹。 2. 在文件夹 selection 上,列表框会列出每个有 GPX 数据的文件到列表框。 3. 我希望能够 select 来自所述列表框的文件并使用另一个按钮回调绘制数据。

我的问题是我不知道如何以编程方式建立此连接。我认为这就像将 ItemsData 设置为在每个文件夹中找到的数据一样简单。也许我没有以正确的方式去做。

下面是我的代码:

classdef HalloweenApp < matlab.apps.AppBase

% Properties that correspond to app components
properties (Access = public)
    UIFigure                  matlab.ui.Figure
    SearchButton              matlab.ui.control.Button
    FoldernameEditFieldLabel  matlab.ui.control.Label
    FoldernameEditField       matlab.ui.control.EditField
    DataListBoxLabel          matlab.ui.control.Label
    DataListBox               matlab.ui.control.ListBox
    PlotButton                matlab.ui.control.Button
end


properties (Access = public)
    gpxfolder % Selected file path for GPX folder
    gpxfiles % Folder with GPX files 
    chosenfile % The specific file for plot
    n % Number of files in the folder
    data % Cell array of information that will be loaded 
end


methods (Access = private)

    % Button pushed function: SearchButton
    function SearchButtonPushed(app, event)
        app.gpxfolder = uigetdir(); % Open dialog box for selecting folder. 
        app.gpxfiles = dir(fullfile(app.gpxfolder, '*.gpx')); % Dir all *gpx in selected folder. 
        % Number of files in that folder.
        app.n = numel(app.gpxfiles);
        app.FoldernameEditField.Value = app.gpxfolder;
        % Populate Listbox with file names
        app.DataListBox.Items = {app.gpxfiles(:).name};
        app.data = cell(1,app.n);
for k=1:app.n
        hold all
        % Creates a colormap based on the amount of files we have
        cmap = hsv(k);
        % Read each file 
        app.data{k} = gpxread(fullfile(app.gpxfolder, app.gpxfiles(k).name)); 
        baseFileName = app.gpxfiles(k).name;
        fullFileName = fullfile(app.gpxfolder, baseFileName);
        fprintf(1, 'Now reading %s\n', fullFileName);
end
    end

    % Button pushed function: PlotButton
    function PlotButtonPushed(app, event)
        app.chosenfile = app.DataListBox.Value;
        assignin('base','chosenfile',app.chosenfile)
        **app.DataListBox.ItemsData = app.data{k};** 
    end
end

% App initialization and construction
methods (Access = private)

    % Create UIFigure and components
    function createComponents(app)

        % Create UIFigure
        app.UIFigure = uifigure;
        app.UIFigure.Position = [100 100 914 678];
        app.UIFigure.Name = 'UI Figure';

        % Create SearchButton
        app.SearchButton = uibutton(app.UIFigure, 'push');
        app.SearchButton.ButtonPushedFcn = createCallbackFcn(app, @SearchButtonPushed, true);
        app.SearchButton.Position = [568 633 100 22];
        app.SearchButton.Text = 'Search';

        % Create FoldernameEditFieldLabel
        app.FoldernameEditFieldLabel = uilabel(app.UIFigure);
        app.FoldernameEditFieldLabel.HorizontalAlignment = 'right';
        app.FoldernameEditFieldLabel.Position = [22 637 77 15];
        app.FoldernameEditFieldLabel.Text = 'Folder name:';

        % Create FoldernameEditField
        app.FoldernameEditField = uieditfield(app.UIFigure, 'text');
        app.FoldernameEditField.Position = [114 633 431 22];

        % Create DataListBoxLabel
        app.DataListBoxLabel = uilabel(app.UIFigure);
        app.DataListBoxLabel.HorizontalAlignment = 'right';
        app.DataListBoxLabel.Position = [65 584 34 15];
        app.DataListBoxLabel.Text = 'Data:';

        % Create DataListBox
        app.DataListBox = uilistbox(app.UIFigure);
        app.DataListBox.Items = {};
        app.DataListBox.Multiselect = 'on';
        app.DataListBox.Position = [114 527 431 74];
        app.DataListBox.Value = {};

        % Create PlotButton
        app.PlotButton = uibutton(app.UIFigure, 'push');
        app.PlotButton.ButtonPushedFcn = createCallbackFcn(app, @PlotButtonPushed, true);
        app.PlotButton.Position = [445 495 100 22];
        app.PlotButton.Text = 'Plot';
    end
end

methods (Access = public)

    % Construct app
    function app = HalloweenApp

        % Create and configure components
        createComponents(app)

        % Register the app with App Designer
        registerApp(app, app.UIFigure)

        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

结束

我在我的代码中增加了我认为会导致问题的行。

您可以使用 "index matching" 进行连接:

baseFileName 存储在与匹配数据具有相同索引的单元格中:

%Your code:
app.data{k} = gpxread(fullfile(app.gpxfolder, app.gpxfiles(k).name)); 

%Additional code:
%$$$
%Store the baseFileName in a cell array.
app.baseFileNames{k} = baseFileName;
%$$$

注意:两个列表中的索引 k 相同。

PlotButtonPushed函数中,可以匹配索引:

baseFileName = app.chosenfile{k};

%Find the index of chosen file (with name baseFileName) in app.DataListBox.Items.
idx = find(strcmp(app.baseFileNames, baseFileName));

%After finding the index, we can get the data.
p = app.data{idx};

完整代码如下:

classdef HalloweenApp < matlab.apps.AppBase

% Properties that correspond to app components
properties (Access = public)
    UIFigure                  matlab.ui.Figure
    SearchButton              matlab.ui.control.Button
    FoldernameEditFieldLabel  matlab.ui.control.Label
    FoldernameEditField       matlab.ui.control.EditField
    DataListBoxLabel          matlab.ui.control.Label
    DataListBox               matlab.ui.control.ListBox
    PlotButton                matlab.ui.control.Button
end


properties (Access = public)
    gpxfolder % Selected file path for GPX folder
    gpxfiles % Folder with GPX files 
    chosenfile % The specific file for plot
    n % Number of files in the folder
    data % Cell array of information that will be loaded

    %$$$
    baseFileNames %Cell array of base file names.
    %$$$
end


methods (Access = private)

    % Button pushed function: SearchButton
    function SearchButtonPushed(app, event)
        app.gpxfolder = uigetdir(); % Open dialog box for selecting folder. 
        app.gpxfiles = dir(fullfile(app.gpxfolder, '*.gpx')); % Dir all *gpx in selected folder. 
        % Number of files in that folder.
        app.n = numel(app.gpxfiles);
        app.FoldernameEditField.Value = app.gpxfolder;
        % Populate Listbox with file names
        app.DataListBox.Items = {app.gpxfiles(:).name};
        app.data = cell(1,app.n);

        %$$$
        app.baseFileNames = cell(1,app.n); %Allocate empty cell 
        %$$$

        for k=1:app.n
            hold all
            % Creates a colormap based on the amount of files we have
            cmap = hsv(k);
            % Read each file 
            app.data{k} = gpxread(fullfile(app.gpxfolder, app.gpxfiles(k).name)); 
            baseFileName = app.gpxfiles(k).name;
            fullFileName = fullfile(app.gpxfolder, baseFileName);            
            fprintf(1, 'Now reading %s\n', fullFileName);

            %$$$
            %Store the baseFileName in a cell array.
            app.baseFileNames{k} = baseFileName;
            %$$$
        end
    end

    % Button pushed function: PlotButton
    function PlotButtonPushed(app, event)
        app.chosenfile = app.DataListBox.Value;
        assignin('base','chosenfile',app.chosenfile)

        %**app.DataListBox.ItemsData = app.data{k};**

        %$$$
        %Assume multiple selection is enabled, so we need a for loop
        for k = 1:length(app.chosenfile)
            baseFileName = app.chosenfile{k};

            %Find the index of chosen file (with name baseFileName) in app.DataListBox.Items.
            idx = find(strcmp(app.baseFileNames, baseFileName));

            %After finding the index, we can get the data.
            %Remember we intitialize data and baseFileNames with the same "k" index
            %   app.data{k} = gpxread(fullfile(app.gpxfolder, app.gpxfiles(k).name));            
            %   app.baseFileNames{k} = baseFileName;
            p = app.data{idx};

            %Do something with the gpx data...
            geoshow(p);
        end
        %$$$
    end
end

% App initialization and construction
methods (Access = private)

    % Create UIFigure and components
    function createComponents(app)

        % Create UIFigure
        app.UIFigure = uifigure;
        app.UIFigure.Position = [100 100 914 678];
        app.UIFigure.Name = 'UI Figure';

        % Create SearchButton
        app.SearchButton = uibutton(app.UIFigure, 'push');
        app.SearchButton.ButtonPushedFcn = createCallbackFcn(app, @SearchButtonPushed, true);
        app.SearchButton.Position = [568 633 100 22];
        app.SearchButton.Text = 'Search';

        % Create FoldernameEditFieldLabel
        app.FoldernameEditFieldLabel = uilabel(app.UIFigure);
        app.FoldernameEditFieldLabel.HorizontalAlignment = 'right';
        app.FoldernameEditFieldLabel.Position = [22 637 77 15];
        app.FoldernameEditFieldLabel.Text = 'Folder name:';

        % Create FoldernameEditField
        app.FoldernameEditField = uieditfield(app.UIFigure, 'text');
        app.FoldernameEditField.Position = [114 633 431 22];

        % Create DataListBoxLabel
        app.DataListBoxLabel = uilabel(app.UIFigure);
        app.DataListBoxLabel.HorizontalAlignment = 'right';
        app.DataListBoxLabel.Position = [65 584 34 15];
        app.DataListBoxLabel.Text = 'Data:';

        % Create DataListBox
        app.DataListBox = uilistbox(app.UIFigure);
        app.DataListBox.Items = {};
        app.DataListBox.Multiselect = 'on';
        app.DataListBox.Position = [114 527 431 74];
        app.DataListBox.Value = {};

        % Create PlotButton
        app.PlotButton = uibutton(app.UIFigure, 'push');
        app.PlotButton.ButtonPushedFcn = createCallbackFcn(app, @PlotButtonPushed, true);
        app.PlotButton.Position = [445 495 100 22];
        app.PlotButton.Text = 'Plot';
    end
end

methods (Access = public)

    % Construct app
    function app = HalloweenApp

        % Create and configure components
        createComponents(app)

        % Register the app with App Designer
        registerApp(app, app.UIFigure)

        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