How can I put a string variable into a Figure Title?

FILE_NAME = '1.A.A';
figure('Name', 'Figure 1')
plot(X, Y, 'LineWidth', 1)
title('FILE %s: X vs Y (dB Mag)', FILE_NAME)
savefig('FILE %s: X vs Y.fig', FILE_NAME)
I have a large number of figures I'm plotting, so
I want to designate the variable FILE_NAME, and save the figures in my Current Folder.
  • Currently, I'm getting an error, "Incorrect number of input arguments"

댓글 수: 2

I think you forgot the sprintf() within the title() function. title() only takes a single text input argument (or Name:Value Pairs) which is why it is throwing that error. Try:
figure('Name', 'Figure 1')
plot(X, Y, 'LineWidth', 1)
title(sprintf('FILE %s: X vs Y (dB Mag)', FILE_NAME))
savefig(sprintf('FILE %s: X vs Y.fig', FILE_NAME))
I'd recommend saving them as PNG files with exportgraphics() or saveas() or export_fig().

댓글을 달려면 로그인하십시오.

 채택된 답변

Star Strider
Star Strider 2020년 5월 14일
You’re almost there!
Add a sprintf call and it does what you want:
title(sprintf('FILE %s: X vs Y (dB Mag)', FILE_NAME))
and:
savefig(sprintf('FILE %s: X vs Y.fig', FILE_NAME))
I tested the title call. Since I don’t want to write the file to my computer, I didn’t test the savefig call. It should work. (It would likely help to include the figure handle as the first argument in the savefig call, just to be certain it’s doing what you want it to, although that’s snot required.)
.

댓글 수: 4

Title worked, but savefig didn't. I implemented it as you've shown, but it gave the error:
Error using save
Unable to open file "FILE_NAME.fig" for output.
Error in matlab.graphics.internal.figfile.FigFile/write (line 32)
save(obj.Path, obj.MatVersion, '-struct', 'SaveVars');
Error in savefig (line 84)
FF.write();
If you are using a Windows system, the colon (:) is likelly the problem.
Substitute the colon with something that Windows doesn’t have a problem with, such as a minus sign:
savefig(sprintf('FILE %s - X vs Y.fig', FILE_NAME))
That worked when I tried it.
That did it! Thank you very much for your help!
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2020년 5월 14일
Try
FILE_NAME = '1.A.A';
hFig = figure; % Bring up a new figure, or hFig = gcf to get an existing figure handle.
hFig.Name = sprintf('%s', FILE_NAME); % or hFig.Name = 'Figure 1' or whatever
hFig.NumberTitle = 'off'; % No "Figure 1" in the title bar.
hFig.WindowState = 'maximized' % Maximize the figure window.
% Plot something...
plot(X, Y, 'LineWidth', 1)
caption = sprintf('FILE %s: X vs Y (dB Mag)', FILE_NAME);
title(caption, 'FontSize', 20);
% Prepare PNG filename:
baseFileName = sprintf('%s.png', FILE_NAME); % Tack on png extension so oeprating system will recognize it.
% folder can be pwd or wherever you want it to go.
folder = pwd; % current folder, or use 'D:\my images\' or wherever...
fullFileName = fullfile(folder, baseFileName);
% Save image to disk:
saveas(hFig, fullFileName); % Save to disk as a PNG format image.

카테고리

도움말 센터File Exchange에서 Printing and Saving에 대해 자세히 알아보기

제품

릴리스

R2017b

질문:

2020년 5월 14일

댓글:

2020년 5월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by