Index exceeds matrix dimensions

I've compared my script to my classmates and have it exactly the same, yet I keep getting an error code and she is not.
The script:
shoxdata = filtdata(:, strmatch('RSHO_X', labels));
shoydata = filtdata(:, strmatch('RSHO_Y', labels));
hipxdata = filtdata(:, strmatch('RGT_X', labels));
hipydata = filtdata(:, strmatch('RGT_Y', labels));
knexdata = filtdata(:, strmatch('RKNE_X', labels));
kneydata = filtdata(:, strmatch('RKNE_Y', labels));
ankxdata = filtdata(:, strmatch('RANK_X', labels));
ankydata = filtdata(:, strmatch('RANK_Y', labels));
heexdata = filtdata(:, strmatch('RHEE_X', labels));
heeydata = filtdata(:, strmatch('RHEE_Y', labels));
metxdata = filtdata(:, strmatch('RMET_X', labels));
metydata = filtdata(:, strmatch('RMET_Y', labels));
hipangle = [];
kneeangle = [];
ankangle = [];
startframe = 1;
endframe = length(shoxdata);
frame = startframe;
while frame <= endframe
footseg = 180 + atand((heeydata(frame)-metydata(frame))/(heexdata(frame)-metxdata(frame)));
trunkseg = atan2d((shoydata(frame)-hipydata(frame)),(shoxdata(frame)-hipxdata(frame)));
thighseg = atan2d((hipydata(frame)-kneydata(frame)),(hipxdata(frame)-knexdata(frame)));
shankseg=atan2d((kneydata(frame)-ankydata(frame)),(knexdata(frame)-ankxdata(frame)));
hipangle = [hipangle:(thighseg-trunkseg)];
kneeangle = [kneeangle:(thighseg-shankseg)];
ankangle = [ankangle:(footseg-shankseg-90)];
frame = frame+1;
end
figure(1)
subplot(3,1,1)
plot(hipangle(151:265))
ylabel('Hip Angle')
subplot(3,1,2)
plot(kneeangle(151:265))
ylabel('Knee Angle')
subplot(3,1,3)
plot(ankangle(151:265))
ylabel('Ankle Angle')

1 个评论

In the future, please format your code by highlighting it, then using the [{}Code] button to format it.

请先登录,再进行评论。

回答(1 个)

If her code runs and yours doesn’t, yours and hers aren’t the same.
I suspect the problem may be here:
hipangle = [hipangle:(thighseg-trunkseg)];
kneeangle = [kneeangle:(thighseg-shankseg)];
ankangle = [ankangle:(footseg-shankseg-90)];
The colon (link) operator by default creates an ascending series with a default increment of 1. If the calculations in parentheses are less than ‘hipangle’, ‘kneeangle’, or ‘ankangle’ on the right-hand-side in the three assignments, the result will be an empty element. So first, check the sizes of those variables. I believe the problem will be readily apparent. That would throw the error in your plot calls.
This may be what you want to do:
hipangle = [hipangle (thighseg-trunkseg)];
kneeangle = [kneeangle (thighseg-shankseg)];
ankangle = [ankangle (footseg-shankseg-90)];
Here, I replaced the colon (:) with spaces (although commas (,) would work as well). See if that works in your code.

2 个评论

That worked!! Thank you so much!
As always, my pleasure!
If my Answer helped you solve your problem, please Accept it!

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Matrix Indexing 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by