フィルターのクリア

Converting String Column Data to Number

3 ビュー (過去 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 Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by