Need to understand error message.

1 次查看(过去 30 天)
I have two sets of code using the same data. One works,the other doesn't. To me they look exactly the same. Can someone point out the differences to me and help me figure out why they don't work.
The reason I need to know is because I am calculating the length of over 1400 trajectories for different years. Some are leap years, others are not. The code that works is for January of a Leap Year. The one that doesn't is from January of a non-Leap Year. Since it is January, the number of trajectories shouldn't change. The data going into the trajectories will change.
In this case, t7Data is the same. 169x496 double. It is from a single source. kmmetersTraveled and kmmetersTraveled1 are both 169x1 double, showing the exact same values in each case. tdumpTime7Day is 169x1 double and also does not change between years. (This is a list of hours from 0 to -168)
This code works. It produces a nice pretty plot of the lengths of each one of my trajectories.
kmmetersTraveled1 = pathdistps(t7Data(:,2),t7Data(:,3),'km');
figure('Visible','on'); set(gcf, 'Position', get(0, 'Screensize'));
annotation('textbox', [0 0.9 1 0.1], ...
'String', 'ABN 7 Day Back Trajectories March 1986', ...
'EdgeColor', 'none','HorizontalAlignment', 'center','FontSize', 14,'FontWeight', 'Bold');
subplot(2,2,1)
plot(tdumpTime7Day,kmmetersTraveled1,'r-');
box off;
xlabel('Time (hours)');xlim([-168 0]);
ylabel('Kilometers Traveled')
hold on;
for j = 6:4:size(t7Data,2)
kmmetersTraveled = pathdistps(t7Data(:,j),t7Data(:,j+1),'km');
plot(tdumpTime7Day,kmmetersTraveled);
end
This code does not work. I see one trajectory, but the others do not plot, even when the variables are the same.
kmmetersTraveled1 = pathdistps(t7Data(:,2),t7Data(:,3),'km');
figure('Visible','on'); set(gcf, 'Position', get(0, 'Screensize'));
annotation('textbox', [0 0.9 1 0.1], ...
'String', 'ABN 7 Day Back Trajectories January 1986', ...
'EdgeColor', 'none','HorizontalAlignment', 'center','FontSize', 14,'FontWeight', 'Bold');
subplot(2,2,1)
plot(tdumpTime7Day,kmmetersTraveled1,'r-');
box off;
xlabel('Time (hours)');xlim([-168 0]);
xlim([-168 0]);
ylabel('Kilometers Traveled')
hold on;
for j = 6:4:size(t7Data,2)
kmmetersTraveled = pathdistps(t7Data(:,j),t7Data(:,j+1),'km');
plot(tdumpTime7Day,kmmetersTraveled);
end
In an assignment A(:) = B, the number of elements in A and B
must be the same.
%or this error message
Index exceeds matrix dimensions.
Aside from differences in the Title and the ylabels, I can see no differences. The problem lies somewhere in the loop, but I don't know where. Just for kicks, I cut and pasted the good code over the bad one, title and labels incl, and it still failed. I am at a loss.
  2 个评论
Stephen23
Stephen23 2019-1-9
编辑:Stephen23 2019-1-9
"Aside from differences in the Title and the ylabels, I can see no differences."
How about this difference (the one that causes that error):
kmmetersTraveled = pathdistps(...)
vs.
kmmetersTraveled(j) = pathdistps(...)
In the first version you reallocate the variable named kmmetersTraveled with whatever pathdistps returns. This will work.
In the second version you try to assign whatever pathdistps returns to one element of the array kmmetersTraveled. If the output of pathdistps is non-scalar (which apparently it is) then this will throw an error, because, as madhan ravi already wrote, you cannot force multiple elements into one element.
Cg Gc
Cg Gc 2019-1-9
编辑:Cg Gc 2019-1-10
Adding the (j) breaks it like this:
In an assignment A(:) = B, the number of elements in A and B
must be the same.
But only with January, not with the other months.
If I copy the broken January code to another month, it works fine. If I copy the good code over to January, it doesn't work. (j) or no (j). The (j) doesn't seem to matter.
I gave up and just retyped it exactly as the working code.I have come to realize that the error code is something with MATLAB 2018b and a few of the January files. It works fine in 2017b, so that is what I am going with.

请先登录,再进行评论。

采纳的回答

madhan ravi
madhan ravi 2019-1-9
编辑:madhan ravi 2019-1-9
Reason: You can't stuff in number f elements in place of one so use cell (containers of the ability to expand [ lookup up cell ])
kmmetersTraveled=cell(1,numel(6:4:size(t7Data,2))); % see preallocation
ctr=1;
for j = 6:4:size(t7Data,2)
kmmetersTraveled{ctr} = pathdistps(t7Data(:,j),t7Data(:,j+1),'km');
plot(tdumpTime7Day,[kmmetersTraveled{ctr}]);
ctr=ctr+1;
end
  1 个评论
Cg Gc
Cg Gc 2019-1-9
It works, and I accepted the answer, but I am still wondering why it works sometimes, but not others. It works fine in other months and other years, but not consistenly across all months and all years. Skipping over February as it is the only thing affected by a leap year, the number of elements going into each variable is the same for all years.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by