a function that draws a plot and use an input for the filename of the plot

2 ビュー (過去 30 日間)
alpedhuez
alpedhuez 2020 年 5 月 31 日
コメント済み: alpedhuez 2020 年 6 月 1 日
To continue,
In my Livescript called test.mlx, I have variables
days
temperatures
I now want to write a function m file that outputs a plot with specification as
plot(days, temperatures)
legend('temperatures')
title('Figure figurenumber. Temperatures')
saveas(gca,'c:\Figure_figurenumber')
Then, what I did is to write a function m file called plot1.m
function plot_test(series1, series2, figurenumber)
plot(series1, series2)
legend('series2')
titlename=['Figure',num2str(figurenumber)]
title(titlename)
end
Then how should one understand the
saveas
part?

採用された回答

Codeshadow
Codeshadow 2020 年 6 月 1 日
See below for sample code:
close all
% Initialize some data
temperatures = rand(1,30)*35;
plot_test(temperatures, 1);
function plot_test(temperatures, figurenumber)
% Plot data
figure()
plot(temperatures);
legend('temperatures');
% Create title
titlename = ['Figure ' num2str(figurenumber)];
title(titlename);
% Create save location
% Make sure to specify the format of the image you want to save at the end.
% pwd will save your file to the current working directory. If you have a specific save location
% in mind, specify the absolute file path.
fileName = ['Figure_' num2str(figurenumber) '.jpg'];
saveas(gcf, fullfile(pwd, fileName))
end

その他の回答 (1 件)

Codeshadow
Codeshadow 2020 年 6 月 1 日
You can use a sprintf command in your function as:
saveLocation = sprintf(‘C:\Figure_%d.jpg’, figurenumber);
saveas(gcf, saveLocation)

カテゴリ

Help Center および File ExchangePrinting and Saving についてさらに検索

タグ

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by