MATLAB Answers

0

change x axis, i have a plot of data from a headset. then i have 5 lines from a game, so i want 0-time from start(green) line till the end(red line), is it possible?

Asked by sindre Røyland on 29 Apr 2015
Latest activity Commented on by Michael Haderlein on 4 May 2015
plot(data(2:end,3),data(2:end,1));
hold on;
plot(data(2:end,3),data(2:end,2), 'k');
plot([Start Start],[0 1], 'g');
plot([mistake1 mistake1],[0 1], 'c');
plot([mistake2 mistake2],[0 1], 'c');
plot([mistake3 mistake3],[0 1], 'c');
plot([mistake4 mistake4],[0 1], 'c');
plot([End End],[0 1], 'r');

  0 Comments

Sign in to comment.

Tags

2 Answers

Answer by pfb
on 29 Apr 2015
Edited by pfb
on 29 Apr 2015
 Accepted Answer

Not sure what your asking for.
Do you need to re-define the limits of your x-axis so that they go from the green to the red line? That would be
xlim([start end]);
Or maybe you want that your scale be 0 at the green line... In that case you simply substract start from all the xdata in your plots. When plotting
plot(data(2:end,3)-start,data(2:end,1));
and likewise for the others.
Otherwise, with focus on the axes containing your plots, like the figure you sent,
% get the handles for the lines
h = findobj(gca,'type','line','linestyle','-');
% loop over the handles
for j = 1:length(h)
% translate the xdata
set(h(j),'xdata',get(h(j),'xdata')-start);
end

  2 Comments

of course! plot(data(2:end,3)-start,data(2:end,1));, i was just stucked in some big ideas!
Thanks!
Not sure I understand even after your reply to Michael.
Translating all your xdata, as illustrated above, puts the origin at start.
The rest seems like a xticklabel or xtick matter.
If you do not translate, but insist on using your plot:
set(gca,'xtick',[54 162],'xticklabel',[0 108]);
otherwise, if you translate, you just need
set(gca,'xtick',[0 108]);
Because the ticklabels would coincide with the real tick positions. Personally, I'd prefer this.

Sign in to comment.


Answer by Michael Haderlein on 29 Apr 2015
Edited by Michael Haderlein on 29 Apr 2015

It's not quite clear what you want.
1) You want the x-axis to only go from the green line to the red line:
xlim(Start, End)
as you have named the respective values this way.
2) You want to get the data which lies in between the green and the red line for further processing:
partofthedata=data(data(:,3)>=Start & data(:,3)<=End,:);
Please note that the exclusion of the first row (you use data(2:end,...)) is not applied here. If this is a problem, first remove this row and then cut out the way I have shown here.
3) A comment on your variable naming: "End" is a very bad variable name. First, it's a keyword and second it has a special meaning as index (even though you have capitalized it - I wouldn't use it).

  3 Comments

I want to keep the data before the green line aswell, and after. So i want this to be blank, and then the 0 axis starts at the green line and the runtime is at the red line. so the green line start at 54 and the red one at 162. 54 = 0, and 162 = 108. And the rest is blank
Ya i know end is a bad word here, but doing it in norwegian and needed to change it quickly
Thanks! and hope that this explain my situation
xlim(Start, End)
would produce an error, though. The limits should be in a vector, at least up to R2012b.
xlim([Start, End])
I agree on avoiding the name End for a variable. Did not spot that.
Oops, thanks for correcting me. Of course it's supposed to be an array. Anyway, guess the issue is already solved.

Sign in to comment.