auto update of text in plot
2 views (last 30 days)
Show older comments
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
Accepted Answer
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
%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 Comments
More Answers (0)
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!