cannot get the title and xlabel plotted in a while loop

4 views (last 30 days)
Mario Christov
Mario Christov on 5 Oct 2016
Commented: Mario Christov on 5 Oct 2016
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

Answers (2)

Pritesh Shah
Pritesh Shah on 5 Oct 2016
Give error message.
Y = -(axis(joy, 2)); it should be,
Y = -(axis([joy, 2]));
  2 Comments
Mario Christov
Mario Christov on 5 Oct 2016
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

Sign in to comment.


Jan
Jan on 5 Oct 2016
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 Comment
Mario Christov
Mario Christov on 5 Oct 2016
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])

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!