gca method to adjust axes properties not working within function

조회 수: 3 (최근 30일)
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
This statement is not inside any function.
(It follows the END that terminates the definition of the function "MakeItLookPretty".)
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
Stephen23
Stephen23 2024년 1월 9일
Do not name your variables "axes".
It works on this forum:
matlabRelease
ans =
matlabRelease with properties: Release: "R2023b" Stage: "release" Update: 5 Date: 01-Dec-2023
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
Tom Chernowsky
Tom Chernowsky 2024년 1월 9일
Yep, it works, the problem was something else entirely. Thank you & sorry to have wasted your time.

댓글을 달려면 로그인하십시오.

채택된 답변

Hassaan
Hassaan 2024년 1월 9일
% 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개)

카테고리

Help CenterFile Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by