MATLAB: Degrees2dms Syntax

degrees2dms

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?

Best Answer

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)
Two help pages to learn indexing: this one and this one
Related Question