Search code examples
matlabconfigurationbootstrappingmatlab-deploymentmatlab-app-designer

Start App from file storing settings and load them at startup


I'm working on an App in app designer. Within the app the user will select a bunch of options before running some calculations. To simplify this process I added a "Save as..." menu so that the user can save the current settings to a file (.mat) and reload them when they open the app the next time.

What I'm trying to achieve is that the user can double click on the previously saved .mat file, which will launch the app and the app will automatically read the double clicked file and load all the settings.

All this needs to happen after the app is compiled and distributed as an executable.

I'm thinking that one way to achieve this is to make a startup window of the app that calls the main window passing the file path as parameter.

Any suggestion would be really appreciated.


Solution

  • Hi, I think I may have a fairly simple, albeit involved, solution for you.

    Brief solution overview (TL;DR)

    Save the settings from the app with an extension other than .mat, e.g. .mydat. Add an App Input Argument and have the startupFcn treat the argument as a file name to a *.mydat file and be sure to also handle the case that the argument is left out. After the first output file is saved, use windows Open with... to select your app. Now double clicking the *.mydat file will open your app's .exe and will provide the file name of the clicked file to the input argument in your startupFcn.


    An example in MATLAB 2018a as a compiled exe on windows 10.

    Ok, to start. Let's setup a simple app (I called it runAppFromData) that takes a string input to an edit field and saves it in a file called 'settingsValues.mydat'. It looks like:

    small app saving prefs

    The callback for the Save button collects the Value into a local variable called value and then saves it to disk:

    % Button pushed function: Save
    function save(app, event)
        value = app.InputField.Value;%#ok
        % User selects save location
        saveLocation = uigetdir();
        % Now just save the value variable to the selected location.
        save(fullfile(saveLocation,'settingsValues.mydat'), 'value', '-mat');
    end
    

    I don't know when appdesigner added the feature to "run app with inputs" but I have it with 2018a:

    appdesigner input args

    We make a single input, fileName that expects a file name as a string (you'll see why below). So add the input and click OK. Then we're sent to "code view" at the startupFcn. Here we'll write the logic that parses the input file. For my simple example app, I load the input file into a struct and then send the value to the edit field:

    % Code that executes after component creation
    function startupFcn(app, fileName)
        if nargin < 2 % app is itself an argument
            % just continue running the application without error
            return
        end
        % fileName is a string, so let's load it into a struc
        S = load(fileName, '-mat');
        % The value field will be there because that is how we wrote it
        app.InputField.Value = S.value;
    end
    

    Note, I performed a nargin check to handle the first-run case (and anytime the app is run from the actual executable).

    MATLAB doesn't care what the file extension is of a matlab file and if you have an unknown file extension, e.g. .mydata, double-clicking the file in windows will ask you to choose the application, which works to your benefit for deployment:

    Windows open file with...


    A couple things to consider.

    When the app is opened from the .exe it will always show the default values. If you want to input some other default values you can edit your windows shortcut Target field to supply a file path for the desired input file (see here). This saves recompiling with new defaults, but the file has to remain somewhere (you can package it with the app too).

    Sorry this answer got soo long! I hope it helps!