auto update of text in plot

1 view (last 30 days)
rakesh kumar
rakesh kumar on 6 Dec 2023
Answered: Dyuman Joshi on 6 Dec 2023
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')
  1 Comment
Mann Baidi
Mann Baidi on 6 Dec 2023
How are you planning to change the value of "sigma"?

Sign in to comment.

Accepted Answer

Dyuman Joshi
Dyuman Joshi on 6 Dec 2023
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().

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!