Clear Filters
Clear Filters

How to customize print file name with function input

6 views (last 30 days)
I have a function including following input items: Filepath, NsperBatch, K. And I would like to use them as part of filename when saving a plot:
print('Magnitude (dB) vs Time vs Frequency Collected in %d RF_bin with Batch %d Avg %d',Filepath,NsperBatch,K,'-dpng');
Receiving some errors as followings:
Error using checkArgsForHandleToPrint
Handle input argument contains nonhandle values.
Error in checkArgsForHandleToPrint
Error in print>LocalCreatePrintJob (line 216)
handles = checkArgsForHandleToPrint(0, varargin{:});
Error in print (line 38)
[pj, inputargs] = LocalCreatePrintJob(varargin{:});
Error in processAverna (line 93)
print('Magnitude (dB) vs Time vs Frequency Collected in %d RF_bin with Batch %d
Avg %d',Filepath,NsperBatch,K,'-dpng');___

Accepted Answer

Walter Roberson
Walter Roberson on 8 Nov 2017
filename = sprintf('Magnitude (dB) vs Time vs Frequency Collected in %d RF_bin with Batch %d Avg %d',Filepath,NsperBatch,K);
print(filename , '-dpng')
  3 Comments
Walter Roberson
Walter Roberson on 9 Nov 2017
I notice that you have a variable named FilePath but you use a %d format specifier for it. That is confusing: I would have expected a %s format specifier.
If you have a qualified file name and want to get just the last part of the name, then
[~, basename, ext] = fileparts(filePath);
then use either basename by itself or [basename ext]
Ivy Chen
Ivy Chen on 13 Nov 2017
Great, thanks! I have updated the code accordingly.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!