Change active elements of Plot Browser from code

4 views (last 30 days)
Hey, I would like to change the active elements of an existing figure with code.
Currently I have to open the Plot Browser and change every element manuelly.
Is there a way to change them with code?

Accepted Answer

Yash
Yash on 28 Mar 2023
Hi Julian Braun,
Yes, you can change the active elements of a figure programmatically using MATLAB code. The following code demonstrates how to change the properties of the X-axis and Y-axis labels, the title, and the legend of a figure:
% Create some sample data and plot it
x = 1:10;
y = sin(x);
% Create a figure to visualize your data
% The variable myFig is a handle to your figure.
myFig = figure;
% Plot your data;
p = plot(x, y)
% Get the axis of your plot
ax = p.Parent;
% With the handle to the axis you can change the active elements
% Change the X-axis label
ax.XLabel.String = 'Time (s)'
% Change the Y-axis label
ax.YLabel.String = 'Amplitude'
% Change the title
ax.Title.String = 'Sine Wave'
You can alternatively set the active elements by the below code as well
% Change the X-axis label
xlabel('Time (s)')
% Change the Y-axis label
ylabel('Amplitude')
% Change the title
title('Sine Wave')
% Change the legend
legend('Sine')
You can also access and modify these properties directly using the gca (get current axes) and gcf (get current figure) functions, which return handles to the current axes and figure, respectively. For example, you can change the font size and font weight of the X-axis label like this:
ax = gca;
ax.XLabel.FontSize = 16;
ax.XLabel.FontWeight = 'bold';
You can similarly modify other properties of the axes, such as the tick labels and tick marks, using the XTick, XTickLabel, YTick, and YTickLabel properties of the axes. For more information on customizing plots in MATLAB, see the documentation on graphics customization:
I hope this helps :)

More Answers (0)

Categories

Find more on Specifying Target for Graphics Output in Help Center and File Exchange

Tags

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!