Clear Filters
Clear Filters

degrees2dms Syntax

4 views (last 30 days)
Jay
Jay on 1 Jul 2019
Commented: Jay on 1 Jul 2019
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
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)

Sign in to comment.

Accepted Answer

Guillaume
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)
Two help pages to learn indexing: this one and this one
  1 Comment
Jay
Jay on 1 Jul 2019
Thank you for the explanation regarding the Syntax (output to the LHS and input to the RHS) Guillaume.
The reason I choose to iteratively convert (degrees2dms) is that it allows me to check the subroutines calculation of each value prior to being populated into an array (opposed to an array-to-array unit conversion).
It also results in not having yet another single use array only to be cleared later on.
I did not know that commands in Matlab are intuitive enough (sequentially) to populate an array output without explicitly having to reference the columns (thank you for showing me this).

Sign in to comment.

More Answers (2)

Steven Lord
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.
  1 Comment
Jay
Jay on 1 Jul 2019
Edited: Jay on 1 Jul 2019
Hi Steven and thanks for the quick response.
Using the explanation degrees2dms my intention was to have the
degrees2dms([C(i,1),C(i,2),C(i,3)]) = B(i,1)
function populate the C(i, 1 2 3) with:
“C(i,1)” populated with the degree values converted from the degree.decimal B(i,3) vector using the degrees2dms function
“C(i,2)” populated with the minutes values converted from the degree.decimal B(i,3) vector using the degrees2dms function
“C(i,3)” populated with the seconds values converted from the degree.decimal B(i,3) vector using the degrees2dms function.
Does that make more sense?

Sign in to comment.


Chirag Nighut
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.
  1 Comment
Jay
Jay on 1 Jul 2019
Edited: Jay on 1 Jul 2019
Hi Chirag,
The using the Syntax per degrees2dms, I wish for the degree.decimal value stored in the B array to be converted to a D M S value in the C array.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!