Converting String Column Data to Number

1 view (last 30 days)
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....

Accepted Answer

Walter Roberson
Walter Roberson on 27 Jun 2021
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 Comments
Tyann Hardyn
Tyann Hardyn on 28 Jun 2021
@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 on 28 Jun 2021
in that case I think you can just remove the 'variablerenamingrule' option.

Sign in to comment.

More Answers (1)

Scott MacKenzie
Scott MacKenzie on 27 Jun 2021
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

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!