degrees2dms Syntax
4 views (last 30 days)
Show older comments
I have the following test code with an error thrown when trying to populate array C using the degrees2dms function:
% Array with original DMS (n x 3 array)
A = [180 342 350 121 125 179; 0 54 41 16 23 59; 3 49 18 20 13 50]'
% Array for Degrees.Decimal values (from array A)
B (6,1) = zeros
% Array for degrees2DMS repopulation
C (6,3) = zeros
%% Initial dms2degrees Array B Population
for i = 1:6
B(i,1) = dms2degrees([A(i,1), A(i,2), A(i,3)])
end
%% Subsequent degrees2dms Array C Population Attempt
for i = 1:6
degrees2dms([C(i,1),C(i,2),C(i,3)]) = B(i,1)
% Error Returned:
% Array indices must be positive integers or logical values.
%Error in Test2_Degrees2dms (line 31)
%degrees2dms([C(i,1),C(i,2),C(i,3)]) = B(i,1)
end
Why am I getting an error stating that negative indicies are being used when I have specified the numberic doman from 1 to 6?
1 Comment
Guillaume
on 1 Jul 2019
Justin's comment posted as an answer moved here:
I noticed (and have since ammended) the degree.decimal array being referenced for values in array C from array A to array B.
Inncorrect Attempt:
degrees2dms([C(i,1),C(i,2),C(i,3)]) = A(i,1)
Corrected Attempt:
degrees2dms([C(i,1),C(i,2),C(i,3)]) = B(i,1)
However, the returned error remains the same.
Array indices must be positive integers or logical values.
Error in Test2_Degrees2dms (line 32)
degrees2dms([C(i,1),C(i,2),C(i,3)]) = B(i,1)
Accepted Answer
Guillaume
on 1 Jul 2019
function(x) = y
has never and never will be valid syntax in matlab. A function call is always on the right hand side of the =.
If you want to populate the first three columns of C with the values returned by degree2dms it's simply:
C(:, 1:3) = degree2dms(B(:, 1)); %no loop needed
As the documentation tells you, the function already returns a 3 column matrix with each row corresponding to the same row of the input. Hence you don't even need to loop over the rows.
Similarly, you didn't need a loop for creating your B:
B(:, 1) = dms2degree(A(:, 1:3));
Also note that
[A(i,1), A(i,2), A(i,3)]
is simply:
A(i, 1:3)
More Answers (2)
Steven Lord
on 1 Jul 2019
What you've written:
degrees2dms([C(i,1),C(i,2),C(i,3)]) = A(i,1)
attempts to create a variable named degrees2dms and assign A(1,1) to an element of it. However, the indices you're using to assign into degrees2dms are all 0, which makes them invalid.
I'm not sure of what you intended this line to do, but if you try explaining it in words we may be able to help write the code to achieve your goal.
Chirag Nighut
on 1 Jul 2019
Degrees2dms takes in degree input (1x1) and converts it into degree minute and second ie. 1x3 output. So here when you pass 1x3 input to your degrees2dms function you get a 3x3 output in return and now that cannot be assigned from A(i,1) and therefore the error.
If possible could you please let me know what your end target is and I can then try to help you out find a solution.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!