Using uicontrol in a figure
이전 댓글 표시
I have a script that I use to plot track data that has three subplots within a figure. I want to use uicontrol to create a toggle button that has a callback to the matlab function grid so that I can toggle the grid on and off. The problem is that I can only toggle the grid in the last subplot but it doesn't work for the first two. Here's most of my plotting script:
%%Set up axes
figure('numbertitle','off')
set(gcf,'Name',filename);
h1 = gcf;
axis off
%%X Plots
subplot(2,2,1)
ax = gca;
%x_axis = get(ax);
xlim([t(1) t(end)])
ylim([-10 30])
ylabel('x [m]')
grid on
hold on
%%Y Plots
subplot(2,2,3)
ay = gca;
%y_axis = get(ay);
h3 = plot([t(1) t(end)],[-half_lane -half_lane],'--',...
[t(1) t(end)],[half_lane half_lane],'--');
set(h3,'Color',[0.5 0 0])
ylabel('y [m]'), xlabel('time [sec]')
xlim([t(1) t(end)])
ylim([-10 10])
grid on
hold on
%%X-Y Plot
subplot(2,2,[2 4])
axy = gca; % get the axis information
xy_axis = get(axy);
car_y = [0 -5.182] + xy_axis.YLim(1);
load 'car.mat'; image([-0.9350 0.9350], car_y, car); clear car; % plot the car for reference
set(gca,'YDir','normal','XDir','reverse'), hold on
xlim([-10 10]);
ylim([-10 25]);
xlabel('y [m]'), ylabel('x [m]')
title(filename,'interpreter','none')
grid on
axis equal
arc_plot([-sensor(1).orientation(2) sensor(1).orientation(1)], (270-sensor(1).orientation(3)), C(1,:));
arc_plot([-sensor(2).orientation(2) sensor(2).orientation(1)], (90-sensor(2).orientation(3)), C(2,:));
arc_plot([-sensor(3).orientation(2) sensor(3).orientation(1)], (90-sensor(3).orientation(3)), C(3,:));
arc_plot([-sensor(4).orientation(2) sensor(4).orientation(1)], -(90+sensor(4).orientation(3)), C(4,:));
xy_axis = get(axy);
%%plot lane
h = plot([-half_lane -half_lane],xy_axis.YLim,'--',[half_lane half_lane],xy_axis.YLim,'--');
set(h,'Color',[0.5 0 0])
%%link axes and insert grid toggle
linkaxes([ax ay],'x');
uicontrol(h1,'Style', 'togglebutton', 'String', 'Grid',...
'Position', [20 20 50 20],...
'Callback', 'grid');
채택된 답변
추가 답변 (1개)
Paulo Silva
2011년 6월 6일
0 개 추천
The grid command like you have in the callback only applies to the current axes so you have to do it for all axes, use the grid command with the axes handles or do something like this:
set(findall(0,'type','axes'),'XGrid','on');set(findall(0,'type','axes'),'YGrid','on') %this turns on all grids on all axes objects.
댓글 수: 2
Walter Roberson
2011년 6월 6일
Abbreviating the above:
set(findall(0,'type','axes'),'XGrid','on','YGrid','on')
Paulo Silva
2011년 6월 6일
thanks Walter :)
카테고리
도움말 센터 및 File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!