Search code examples
matlabfileloadmatlab-app-designer

How to load seperate .mat files using a drop down component in App Designer?


I have separate .mat files, trials in a study, which consits of the same variables but the value change between files.

I want to use a drop-down component in MATLAB app designer to select a file, load its variables and display various plots.

Any ideas how I could do this? Thank you in advance.

I had been loading a single file as a property like this: var = load('Trial1.mat') This code worked.

So then I tried to use this layout to load the files using the drop down menu but it didn't work..

 function SelectFileDropDownValueChanged(app, event)
            value = app.SelectFileDropDown.Value;
            if strcmp(value,'Trial 1')
                var = load('Trial1.mat');
            elseif strcmp(value,'Trial 2')
                var = load('Trial2.mat');
            elseif strcmp(value,'Trial 3')
                var = load('Trial3.mat');
            elseif strcmp(value,'Trial 4')
                var = load('Trial4.mat');
            elseif strcmp(value,'Trial5')
                var = load('Trial5.mat');
            end

Any ideas how I could do this? Thanks in advance!


Solution

  • You can do it as follows:

    Add a new property named var to the App class.

    You can add a private (or public) property in the EDITOR tab of the Designer:
    enter image description here

    Change the property name to var (matching your code sample).

    Properties code block:

    properties (Access = private)
        var % Description: store loaded variables
    end
    

    Now var is a class member.
    The App Designer is based on Object-Oriented MATLAB Programming.
    Accessing var is possible only withing the code of the App class (because it's a private member).
    Accessing var property is as follows: app.var (app is a reference to class' object).
    Note that app is the first parameter of class methods (as in SelectFileDropDownValueChanged method).

    Modified SelectFileDropDownValueChanged code:

    % Value changed function: SelectFileDropDown
    function SelectFileDropDownValueChanged(app, event)
        value = app.SelectFileDropDown.Value;
    
        if strcmp(value,'Trial 1')
            app.var = load('Trial1.mat');
        elseif strcmp(value,'Trial 2')
            app.var = load('Trial2.mat');
        elseif strcmp(value,'Trial 3')
            app.var = load('Trial3.mat');
        elseif strcmp(value,'Trial 4')
            app.var = load('Trial4.mat');
        elseif strcmp(value,'Trial5')
            app.var = load('Trial5.mat');
        end           
    end