gca method to adjust axes properties not working within function
3 views (last 30 days)
Show older comments
Hello,
I have a function which adjusts some properties of figures after they have been plotted. The function takes the axes graphical object as argument. It goes like this:
function MakeItLookPretty(ax)
ax.YAxis.FontName='Open Sans Bold'
ax.YAxis.FontSize=13
ax.XAxis.TickLength=[0 0]
% and so on...
end
I used to be able to call the function:
axes = gca
MakeItLookPretty(axes)
and it adjusted properties of axes of the current figure. This, however, stopped working since I updated to MATLAB2023b. Calling the function does not throw an error, it seemingly does nothing. Interestingly, if I run the block of code "standalone" (not inside a function) it adjusts the axes perfectly.
What am I missing?
2 Comments
Stephen23
on 9 Jan 2024
Do not name your variables "axes".
It works on this forum:
matlabRelease
plot(rand(5,2))
MakeItLookPretty(gca)
function MakeItLookPretty(ax)
ax.YAxis.FontName='Open Sans Bold';
ax.YAxis.FontSize=13;
ax.XAxis.TickLength=[0,0];
% and so on...
end
Accepted Answer
Hassaan
on 9 Jan 2024
% Call the sample plotting function
createSamplePlot();
% Sample plotting function
function createSamplePlot()
% Create some sample data
x = linspace(0, 2*pi, 100);
y = sin(x);
% Plot the data
plot(x, y);
title('Sample Plot');
% Get the current axes handle
ax = gca;
% Call the custom function to make the plot look pretty
MakeItLookPretty(ax);
end
% Custom function to modify the appearance of the plot
function MakeItLookPretty(ax)
% Modify various properties of the axes
ax.YAxis.FontName = 'Open Sans Bold';
ax.YAxis.FontSize = 13;
ax.XAxis.TickLength = [0 0];
% Add more customization as needed...
end
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
Feel free to contact me.
0 Comments
More Answers (0)
See Also
Categories
Find more on Creating, Deleting, and Querying Graphics Objects 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!