Changing tick labels on x axis

1 view (last 30 days)
Hi there,
I'm trying to change the labels on my plot's x-axis, but without any success. I've already read on here through the issues others have had and tried them but it seems I'm doing something wrong.
  • When I set my cfg.xlim = ([-0.1 1.5]), it does limit the axis to these but I only get ticks at -0.1, 0.5, 1 and 1.5, which in my case isn't very helpful as I'm looking into milliseconds differences.
  • When I try something like cfg.limits = ([-0.1:0.2:1.5]), it cuts my axis at 0.9.
  • When I try setting cfg.xticks = ([-0.1:0.2:1.5]), it simply does nothing.
  • I tried the below code as well, creating a double of the ticks I'd want.
Could anyone point me to where the problem is please?
Note, the data is EEG data and I'm using singleplotER, which is from the Fieldtrip toolbox. Nevermind, the axis changes should still work, reportedly...
cfg = [];
cfg.showlabels = 'yes';
cfg.fontsize = 15;
cfg.linewidth = 1.5
cfg.layout = 'EEG1010.lay';
cfg.channel = 'CPz';
cfg.ylim = ([-6.5 6.5])
axisticks = ([-0.1:0.2:1.5])
cfg.xmin = ([min(axisticks)])
cfg.xmax = ([max(axisticks)])
cfg.xticks = ([axisticks])
ft_singleplotER(cfg, ER_S_av_RT, ER_NS_av_RT);
axes = gca;
line([0 0],[-6.5 6.5],'Color','Black')
line([0.553 0.553],[-6.5 6.5],'Color','Black')
line([1.149 1.149],[-6.5 6.5],'Color','Black')
line([-1 21],[0 0],'Color','Black')
set(gca,'box','on');
ylabel('Electric Potential (\muV)')
xlabel('Time (s)')
cfg.showlegend = 'yes'
legend({'Condition 1', 'Condition 2'},'Location','southeast','FontSize',12);
title('Difference between conditions')

Accepted Answer

Jan
Jan on 5 Jun 2022
This is pure voodoo:
axisticks = ([-0.1:0.2:1.5])
cfg.xmin = ([min(axisticks)])
cfg.xmax = ([max(axisticks)])
cfg.xticks = ([axisticks])
You are defining fields for the control struct cfg which are not used by ft_singleplotER. Read the help section of this function instead of guessing, which arguments are used.
Avoid cargo-cult-porgramming. Parentheses and brackets have a function in Matlab. Do not use them randomly:
axisticks = ([-0.1:0.2:1.5])
cfg.xmin = ([min(axisticks)])
% Leaner and smarter:
axisticks = -0.1:0.2:1.5; % [] is the operator for concatenation
cfg.xmin = -0.1; % Or at least: min(axisticks)
It is completely useless to set a field of cfg after ft_singleplotER ran.
axes = gca;
It is a bad idea to shadown the function called "axes" by a variable. This is not a bug, but it can cause very confusing behavior later.
You did not exactly explain, what you want to achieve. But I guess:
...
ft_singleplotER(cfg, ER_S_av_RT, ER_NS_av_RT);
line([0 0],[-6.5 6.5],'Color','Black')
line([0.553 0.553],[-6.5 6.5],'Color','Black')
line([1.149 1.149],[-6.5 6.5],'Color','Black')
line([-1 21],[0 0],'Color','Black')
set(gca, 'box', 'on', ...
'XTick', -0.1:0.2:1.5);
...
  1 Comment
Zlatomira Ilchovska
Zlatomira Ilchovska on 5 Jun 2022
Thank you so much for the helpful advice and guidance, Jan! I guess I often run into similar-style problems, due to starting my learning from very complex code I needed to figure out by myself, and only afterwards starting to learn the basics it operated on.
Yes, you correctly guessed I was trying to just add the ticks on the x-axis, at smaller divisions. The above code explains it well and serves the purpose. Thank you again!

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 5 Jun 2022
Try the xticklabels function.

Tags

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!