Search code examples
matlabmatlab-app-designer

Error in save as type using uiputfile - Matlab


In app designer I have two buttons one to declare the working folder:

function setSaveLocationButtonPushed(app, event)
     app.path = uigetdir()             
end

The other one to save an image

function saveButtonPushed(app, event)
       pathSave = app.path;  
       [file, pathSave] = uiputfile([pathSave,'*.jpg']);
…
            
        end

Why do I get in the save as type also the app.path? (as shown in image)

matl


Solution

  • Your code [pathSave,'*.jpg'] concatenates the path and the filter and then passes the result as the only argument to the uiputfile function. This argument tells the function what file filter to use.

    Instead of storing the chosen directory, make it change the current directory. The file selection UI always opens in the current directory.

    function setSaveLocationButtonPushed(app, event)
       p = uigetdir;
       cd(p)         
    end
    
    function saveButtonPushed(app, event)  
       [file, pathSave] = uiputfile('*.jpg');
       …       
    end
    

    If you don’t want to change the current directory for the whole application, you can change it just before calling the uiputfile function, and change it back afterward:

    function saveButtonPushed(app, event)
       p = cd(app.path);
       [file, pathSave] = uiputfile('*.jpg');
       cd(p);
       …       
    end