MATLAB Answers

Need to understand error message.

4 views (last 30 days)
Cg Gc
Cg Gc on 9 Jan 2019
Edited: Cg Gc on 10 Jan 2019
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 Comments

Stephen Cobeldick
Stephen Cobeldick on 9 Jan 2019
"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 on 9 Jan 2019
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.

Sign in to comment.

Accepted Answer

madhan ravi
madhan ravi on 9 Jan 2019
Edited: madhan ravi on 9 Jan 2019
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 Comment

Cg Gc
Cg Gc on 9 Jan 2019
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.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by