Need to understand error message.
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Stephen23
am 9 Jan. 2019
Bearbeitet: Stephen23
am 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.
Akzeptierte Antwort
madhan ravi
am 9 Jan. 2019
Bearbeitet: madhan ravi
am 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
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Data Preprocessing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!