Degrees-Minutes-Seconds Separation

조회 수: 2 (최근 30일)
Matthew Tyler Jeffries
Matthew Tyler Jeffries 2019년 3월 26일
답변: Are Mjaavatten 2019년 3월 27일
I would like to take a column of data (as a string array) that contains degrees-minutes-seconds, and separate each of the values.
I'm starting with this:
23°10'5''
24°20'10''
25°30'15''
26°40'20''
And I would like to have this:
[23 10 5; 24 20 10; 25 30 15; 26 40 20]

채택된 답변

Are Mjaavatten
Are Mjaavatten 2019년 3월 27일
It simplifies things if you use a doubke quote (") instead of two single qotes ('') to denote seconds. I enclose the modified input as the attached file "jeffries.txt". The foillowing code will then do the job:
fid = fopen('jeffries.txt');
c = textscan(fid,'%s','delimiter','\n');
fclose(fid);
c = c{1};
A = zeros(4,3);
for i = 1:length(c)
s = c{i};
deg = strfind(s,'°');
min = strfind(s,'''');
sec = strfind(s,'"');
A(i,1) = str2num(s(1:deg-1));
A(i,2) = str2num(s(deg+1:min-1));
A(i,3) = str2num(s(min+1:sec-1));
end
disp(A)

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by