Clear Filters
Clear Filters

Using the dms2degree Command Sequentially to Populate an Array using a Sub-routine

3 views (last 30 days)
How to I use the dms2degree (mapping toolbox) command for array elements using a for statement?
Eg.
n=6
A = [180 342 350 121 125 179; 0 54 41 16 23 59; 3 49 18 20 13 50]'
B (6,1) = zeros
for i =1:n
tempVal = dms2degree(A(i,1), (i,2), (i,3)) % Syntax error when trying to specify row & element of A
% Populates degree.decimals into array B iteratively
B(i,1) = tempVal
end
Thanks

Answers (2)

James Tursa
James Tursa on 27 Jun 2019
Edited: James Tursa on 27 Jun 2019
With a for-loop, you need to use A in all of your indexing and use the [ ] brackets to form a vector input (and spell the function name correctly). E.g.,
tempVal = dms2degrees([A(i,1), A(i,2), A(i,3)])
Or just
tempVal = dms2degrees(A(i,1:3))
Also, you need to pre-allocate B differently:
B = zeros(n,1);
But of course you can do the whole thing without a for-loop:
B = dms2degrees(A);

Jay
Jay on 28 Jun 2019
Edited: Jay on 28 Jun 2019
What does one do when the dms values exceed a n x 3 matrix?
Example:
A = [180 342 350 121 125 179; 0 54 41 16 23 59; 3 49 18 20 13 50 ;180 342 350 121 125 179; 0 54 41 16 23 59; 5 50 20 20 15 48]'
B=NaN(15,15)
B((4:9),(2:7))=A
Also, why does the randi command not work using dimesnional specifiers (tried using both the Nan and zero arrays)
A = [180 342 350 121 125 179; 0 54 41 16 23 59; 3 49 18 20 13 50 ;180 342 350 121 125 179; 0 54 41 16 23 59; 5 50 20 20 15 48]'
B=NaN(15,15)
B = randi(:,1:4)
A = [180 342 350 121 125 179; 0 54 41 16 23 59; 3 49 18 20 13 50 ;180 342 350 121 125 179; 0 54 41 16 23 59; 5 50 20 20 15 48]'
B(15,15)=zeros
B = randi(:,1:4)
Both return the following error:
Undefined function or variable 'randi'.
Error in Test1 (line 13)
B = randi(:,1:4)
I also appologise not using the exact example initially as I did not know the requirements for using the dms2degrees command.
  1 Comment
Jay
Jay on 28 Jun 2019
After rereading the documentation (dms2degrees), I don't think the dms2degrees function can do what is required without first:
  1. Creating a seperate array (n x 3).
  2. Duplicating the values in the n x 3 array (input).
  3. Converting each value iteratively.
  4. Using a seperate subroutine to calculate the value as required.
  5. Use the degrees2dms command (returned value)
  6. Split the calculated dms value so each unit has their own respective column (per the read in value).
  7. Repopulate the respective elements.
If anyone has a better way, please let me know.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!