Search code examples
matlabmatlab-figurematlab-guidematlab-deployment

Name matlab figure file differently


I want to print out matlab figure differently and let user to input file name each time. I am using print function which automatically save the name of the function that is define in string. so far I have this. I got this answer from here.

filename = gcf;
print(filename,'myfilename','-dpng','-r30');

which print out the figure with myfilename. I was wondering, is there anyway I can let users to input that string every times it prints out the figure? Always appreciate the help from stack overflow. Thanks


Solution

  • There are a number of ways to do this.

    • uiputfile - Uses a real save dialog that would warn in the case of overwriting an existing file etc.

      [fname, pname] = uiputfile('filename.png', 'Please select a file location');
      
      % Make sure the user didn't hit cancel
      if isequal(fname, 0) || isequal(pname, 0)
          return;
      end
      
      % Create the filename
      filename = fullfile(pname, fname);
      
      print(gcf, filename, '-dpng', '-r30');
      
    • input - Prompt the user to enter a filename at the command window.

      filename = input('Please enter a filename:');
      print(gcf, filename, '-dpng', '-r30');
      
    • inputdlg - opens a GUI prompt for the user to enter the desired filename.

      filename = inputdlg('Please enter a filename');
      print(gcf, filename, '-dpng', '-r30');
      

    I would recommend the uiputfile approach, personally