"Index exceeds matrix dimensions" Error. solution please.

x1=60;x2=40; y1=40; y2=60;
d = sqrt((x2-x1)^2+(y2-y1)^2);
a = atan2(-(x2-x1),(y2-y1));
b = asin(d/2/r);
c = linspace(a-b,a+b);
e = sqrt(r^2-d^2/4);
x = (x1+x2)/2-e*cos(a)+r*cos(c);
y = (y1+y2)/2-e*sin(a)+r*sin(c);
axis([0 150 0 150])
hold on
plot(x,y,'r');
axis([0 150 0 150])
h1 = plot(x(1),y(1),'bs','MarkerSize',7,'MarkerFaceColor','r');
for n = 1:numel(t)
set(h1, 'XData', x(n), 'YData', y(n)); %HERE
drawnow %// refresh figure
end
The error is displayed in the highlighted line.
suggest solution. (what should be the value of t here)
Here, the program runs even without specifying the value of t. how is it possible?
Can the speed of the pointer be controlled (in terms of frequency/velocity) by the user given input (from dialog box)?

1 Comment

@h b: Please format your code using the "{} code" button. It is easier to help you, when the code is readable.

Sign in to comment.

 Accepted Answer

You have an old t variable sitting around or passed in,and it is longer than your x is.

14 Comments

h b
h b on 7 Nov 2016
Edited: h b on 7 Nov 2016
is it also possible to increase the matrix dimension such that it matches my index value (i.e. t )?
can i resize my matrix size from 1*100 to 1*180 for an index value of 180.
Yes, you can extend x and y:
nt = numel(t);
x(end+1:nt) = nan;
y(end+1:nt) = nan;
but wouldn't it be much easier to just use a variable that has some relevance to the code you presented?
for n = 1:numel(x)
thanks. yes i had got the solution.
Could you rectify the error in the below mentioned code as the pointer stops without trailing the entire path.
t=linspace(0,40,180)
x3=linspace(54,54,180);% left
y=linspace(0,40,180);
a1=54;a2=44; b1=40; b2=60;
d = sqrt((a2-a1)^2+(b2-b1)^2); % Distance between points
a = atan2(-(a2-a1),(b2-b1)); % Perpendicular bisector angle
b = asin(d/2/r); % Half arc angle
c = linspace(a-b,a+b); % Arc angle range
e = sqrt(r^2-d^2/4); % Distance, center to midpoint
q = (a1+a2)/2-e*cos(a)+r*cos(c); % Cartesian coords. of arc
z = (b1+b2)/2-e*sin(a)+r*sin(c);
nt = numel(t);
q(end+1:nt) = nan;
z(end+1:nt) = nan;
xa=linspace(44,0,180);
ya=linspace(60,60,180);
x111=[ x3 q xa ]
y111=[ y z ya ]
axis([0 150 0 150]);
hold on;
h1 = plot(x111(1),y111(1),'bs','MarkerSize',7,'MarkerFaceColor','r'); %// plot initial position of object 1
for n = 1:numel(t)
set(h1, 'XData', x111(n), 'YData', y111(n)); %// update position of object 2
drawnow %// refresh figure
end
Do not extend q and z with nan. Instead, you should be using
for n = 1:numel(x111)
You do not not ask for any trailing at all. If you want trailing then
for n = 1:numel(t)
set(h1, 'XData', x111(1:n), 'YData', y111(1:n)); %// update position of object 2
drawnow %// refresh figure
end
Or better yet, look at animatedline()
any way to control the speed of the pointer as per the user input?
fps = input('How many frames per second? ')
delay = 1/fps;
and replace
drawnow
with
pause(delay)
Thank you. i wish to user input speed in terms of velocity(distance/time) from excel. in order to achieve the same, is it possible to divide my matlab output figure into cells of desired dimension
Yes, it is possible to divide your output figure into cells of desired dimension. It is also a lot of work.
Easier is to calibrate your output rate to the virtual distance to be traveled.
See attached.
The first parameter to the function is r, the radius, which you never defined in your posting so I decided it must be something you wanted to adjust.
The second parameter is v, the velocity. The total distance to be traveled is about 120, so approximately 120/v will give the number of seconds the output will take to plot.
An easier version of the code would be to say that the distance between adjacent points is "close enough" to equal, and so to divide the calculated time by the number of points and advance equally. It turns out that the overhead of doing that gets fairly high for higher velocities (shorter wait times.) And because you are traveling on a curve, you should not really be calling the distances to be equal.
The version of the code I attach calculates the distances between adjacent points by Euclidean formula, totals the distances, uses the velocity to calculate times. Then, to allow the plot to proceed at constant linear velocity, it divides the time up into a number of chunks and figures out where on the curve (which sample) one would be at after that time. Then it plots with the trail increasing one chunk at a time.
h b
h b on 11 Nov 2016
Edited: h b on 11 Nov 2016
("The version of the code I attach..." Have u provided any attached file for reference? ..)
Thanks for the approach.
Thank you so much.

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Performance in Help Center and File Exchange

Tags

Asked:

h b
on 4 Nov 2016

Commented:

h b
on 11 Nov 2016

Community Treasure Hunt

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

Start Hunting!