auto update of text in plot

2 次查看(过去 30 天)
i have a plot for sigma =0.05. i want the text in the plot should display sigma = 0.03 when i change the value of sigma as 0.03 and save it as new png file as sigma=0.03
sigma=0.05
me3=rand(1,2000);
me3d=rand(1,2000);
X=1:2000
plot(X, me3, '-*', 'Color', 'red', 'LineWidth', 2, 'MarkerIndices', 1:1000:length(me1))
hold on
plot(X, me3d, '-*', 'Color', 'blue', 'LineWidth', 2, 'MarkerIndices', 1:1000:length(me1))
text(1000, 0.55, '\sigma = 0.05', 'FontSize', 14, 'FontWeight', 'bold')
ylabel(' eigen frequencies', 'FontWeight', 'bold', 'FontSize', 16)
xlabel('Sample size', 'FontWeight', 'bold', 'FontSize', 16)
set(gca, 'FontSize', 16, 'FontWeight', 'bold')
saveas(gcf, 'mean sigma = 0.05.png')

采纳的回答

Dyuman Joshi
Dyuman Joshi 2023-12-6
Convert the script to a function (make appropriate change accordingly in doing so) and call the function for different values of sigma -
for sigma = [0.03 0.05]
myfun(sigma)
end
%Check the contents of the current folder
ls
'mean sigma = 0.03.png' 'mean sigma = 0.05.png'
%Function
function myfun(sigma)
me3=rand(1,2000);
me3d=rand(1,2000);
X=1:2000;
figure
plot(X, me3, '-*', 'Color', 'red', 'LineWidth', 2, 'MarkerIndices', 1:1000:length(me3))
hold on
plot(X, me3d, '-*', 'Color', 'blue', 'LineWidth', 2, 'MarkerIndices', 1:1000:length(me3))
text(1000, 0.55, sprintf('\\sigma = %0.2f', sigma), 'FontSize', 14, 'FontWeight', 'bold')
ylabel(' eigen frequencies', 'FontWeight', 'bold', 'FontSize', 16)
xlabel('Sample size', 'FontWeight', 'bold', 'FontSize', 16)
set(gca, 'FontSize', 16, 'FontWeight', 'bold')
saveas(gcf, sprintf('mean sigma = %0.2f.png', sigma))
end
Changes made -
Generalised the text to be displayed on the plot and filename of the image to be saved as, by including sprintf().

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by