cannot get the title and xlabel plotted in a while loop

1 次查看(过去 30 天)
Hello, I am using the following code to update the vertical positions on a figure plot of a target marker(p1), joystick marker(p2), and a connecting line between both(p3). I would like to also display the time as title and the position coordinates as xlabel. It worked last week, I don't remember what I changed/deleted since then, but I can't fix it now/ realize what the issue is ? Any suggestions ? Thanks in advance !
k = 1;
while k <= 1000
Y = -(axis(joy, 2));
set(p1,'YData',y1_shifted_abs(k));
set(p2,'YData',Y);
set(p3,'YData',[y1_shifted_abs(k) Y])
time = datestr(now);
title(strcat('Clock time: ',time(end-7:end)));
xlabel({strcat('Target position: ',' ',num2str(y1_shifted_abs(k)));strcat('Joystick position: ',' ',num2str(Y));strcat('\bf Gap: ',' ',num2str(abs(y1_shifted_abs(k) - Y)))});
drawnow
k = k + 1;
end

回答(2 个)

Pritesh Shah
Pritesh Shah 2016-10-5
Give error message.
Y = -(axis(joy, 2)); it should be,
Y = -(axis([joy, 2]));
  2 个评论
Mario Christov
Mario Christov 2016-10-5
Writing "Y = -(axis(joy, 2));" is correct. It does not produce an error. It is the correct way of getting the value along the second axis as stated in the documentation

请先登录,再进行评论。


Jan
Jan 2016-10-5
Do you get an error message? Do the results differ from your expectation? Please mention the problem you have.
I think this is nicer:
xlabel({sprintf('Target position: %g', y1_shifted_abs(k)); ...
sprintf('Joystick position: %g', Y); ...
sprintf('\bf Gap: %g', abs(y1_shifted_abs(k) - Y))});
but I do not see a connection to a problem.
  1 个评论
Mario Christov
Mario Christov 2016-10-5
No error messages are returned. The while-loop works well, the marker moves as expected and the joystick too. It's just that the title and xlabel do not get displayed. I tried to execute the commands on the figure after the loop is finished and it still does not work; if I try on another figure plot, it does work => hence it's something with the plot that I cannot figure out what. Before starting the while-loop I create the figure with the following script:
figure
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
axis([-1 1 0 1]);
set(gca,'visible','off'); % hide all axes lines
hold on
line([-0.1 -0.1],[0 1],'LineWidth',4); % line([x1 x2],[y1,y2])
line([0.1 0.1],[0 1],'LineWidth',4);
p1 = plot(-0.1,0.5, 'o','MarkerFaceColor','w','MarkerSize',18,'MarkerEdgeColor','k'); % TARGET MARKER
p2 = plot(0.1,0.5,'o','MarkerSize',18,'MarkerEdgeColor','w','MarkerFaceColor','k'); % JOYSTICk MARKER
p3 = line([-0.1 0.1],[0.5,0.5],'LineWidth',3,'LineStyle',':','Color',[0 0 0]); % CONNECTING LINE, black
hold off
set(p1,'XData',-0.1);
set(p2,'XData',0.1);
set(p3,'XData',[-0.1 0.1])

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by