matlabmatlab-app-designer

Can a MATLAB App Designer app launch a script which saves figures?


App Designer doesn't support saveas, savefig, or print, but is there a way to use it to launch a MATLAB program which does?

I am working with a MATLAB program which has thousands of lines of code, not all of it mine. This program saves figures for later reference, some in .fig format and some as PDFs. It works well, but there are many options to choose for each run, currently done by editing values at the top of the main script. I'd like to add a GUI to simplify that.

I know that there are other ways, but App Designer seems to be the "modern" thing to use. Can I use it as a launcher for the existing script? If I just call it as shown below, the graphic limitations of App Designer apply to the whole script, and it does all of the calculations but fails at the first "print". I'm hoping there's another way.

% Callback function
function GoButton_2Pushed(app, event)
    % Save values for GUI restart.
    setKeySaveState(app);            
    % Use a struct to pass values to the model.
    scriptVars = app.modelVars;
    % Run
    The_Name_of_My_Script
end

I could just have the GUI write the structure to a file in json format, launch the main script by hand, and have it read the file. But that just seems silly.

Edit: here are the error messages from the MATLAB command window:

Error using print (line 79) Functionality not supported with figures created with the uifigure function. For more information, see Graphics Support in App Designer.

Error in MapsCoralCoverClean>oneMap (line 298) print('-dpdf', '-r200', outFile);

Error in MapsCoralCoverClean (line 70) oneMap(13, activeLatLon(:, 1), activeLatLon(:, 2), events85_2010(activeReefs), [], jet, tName, outFile, false);

Error in A_Coral_Model_170118 (line 780) MapsCoralCoverClean(fullMapDir, Reefs_latlon, toDo, lastYearAlive, ...

Error in ModelGUI_2017a/GoButton_2Pushed (line 465) A_Coral_Model_170118 Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 310) Error while evaluating Button PrivateButtonPushedFcn.


Solution

  • I find it best practice to always explicitly provide handles to graphics objects, as relying on MATLAB's current figure and current axes is often inconsistent and can lead to issues.

    Unless explicitly provided, print will save the current figure according to the other parameters passed. In this case, your uifigure window is remaining the current figure during execution of your other processing functionality, causing print to error out because it is not yet implemented for some of the new graphics objects (why, MathWorks, why!?). To fix this, pass the output of your processing functions' figure calls as the first input to print.