Converting String Column Data to Number

조회 수: 1 (최근 30일)
Tyann Hardyn
Tyann Hardyn 2021년 6월 27일
댓글: Tyann Hardyn 2021년 7월 2일
Hi everyone, i wanna ask. So i have a data readed by using this code :
filename = 'D:\KULIAH PASCASARJANA ITB\Bimbingan Tesis\abk198911dmin.txt';
startRow = 14;
formatSpec = '%10{yyyy-MM-dd}D%13s%4f%13f%10f%10f%f%[^\n\r]';
fileID = fopen(filename,'r');
textscan(fileID, '%[^\n\r]', startRow-1, 'WhiteSpace', '', 'ReturnOnError', false, 'EndOfLine', '\r\n');
dataArray = textscan(fileID, formatSpec, 'Delimiter', '', 'WhiteSpace', '', 'TextType', 'string', 'EmptyValue', NaN, 'ReturnOnError', false);
dataArray{2} = strtrim(dataArray{2});
fclose(fileID);
DATE = dataArray{:,1};
TIME = dataArray{:,2};
DOY = dataArray{:,3};
ABKX = dataArray{:,4};
ABKY = dataArray{:,5};
ABKZ = dataArray{:,6};
ABKF = dataArray{:,7};
d = str2double (TIME);
c = str2double(strrep(TIME,':',''));
for i = 1:length(c)
Converted1 = sprintf('%c%c:%c%c:%s', c(i));
Converted2 = [c(1:2), ':', c(3:4), ':', c(5:10)];
end
I cant stop thinking how to make the TIME data column to become number data format by using this format 'hh:mm:ss' because it is a string data format which cannot be able to be converted to number format by me. I have tried so many times by using that c variable (str2double(strrep(TIME,':',''))) and the other, but it doesnt work. So please help me out everyone, thank you very much.
If the string formatted TIME data column had been converted to become number format (hh:mm:ss) so then i can plot it....

채택된 답변

Walter Roberson
Walter Roberson 2021년 6월 27일
Why go through all that trouble?
filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/666885/abk198911dmin.txt';
T = readtable(filename, 'headerlines', 12, 'variablenamingrule', 'preserve');
T.DT = T.DATE + T.TIME;
ABKX = T{:,4};
plot(T.DT, ABKX)
  댓글 수: 5
Tyann Hardyn
Tyann Hardyn 2021년 6월 28일
@Walter Roberson , Im using Matlab R2019.a (9.6.0.1072779). I dont know whether it support that variablenamingrule or not, but its 2019 version, Sir. It is not that old.
Walter Roberson
Walter Roberson 2021년 6월 28일
in that case I think you can just remove the 'variablerenamingrule' option.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Scott MacKenzie
Scott MacKenzie 2021년 6월 27일
No need to use textscan. Just read the data into a MATLAB table. The 2nd column (TIME) will be treated as durations. You can then use the hours function to get the time as a number for plotting. Here's a quick demo:
f = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/666885/abk198911dmin.txt';
T = readtable(f);
head(T)
T.TIME(1:5)
hours = hours(T.TIME(1:5))
Output:
ans =
8×8 table
DATE TIME DOY ABKX ABKY ABKZ ABKF x_
__________ ________ ___ _____ ____ _____ _____ ___
1989-11-01 00:00:00 305 11575 746 51129 99999 NaN
1989-11-01 00:01:00 305 11575 747 51132 99999 NaN
1989-11-01 00:02:00 305 11576 750 51134 99999 NaN
1989-11-01 00:03:00 305 11576 749 51135 99999 NaN
1989-11-01 00:04:00 305 11575 750 51137 99999 NaN
1989-11-01 00:05:00 305 11572 752 51137 99999 NaN
1989-11-01 00:06:00 305 11583 753 51135 99999 NaN
1989-11-01 00:07:00 305 11579 751 51137 99999 NaN
ans =
5×1 duration array
00:00:00
00:01:00
00:02:00
00:03:00
00:04:00
hours =
0
0.016667
0.033333
0.05
0.066667
  댓글 수: 1
Tyann Hardyn
Tyann Hardyn 2021년 7월 2일
Thank you very much, Sir.... it helped me a lot

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by