Effacer les filtres
Effacer les filtres

Converting String Column Data to Number

3 vues (au cours des 30 derniers jours)
Tyann Hardyn
Tyann Hardyn le 27 Juin 2021
Commenté : Tyann Hardyn le 2 Juil 2021
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....

Réponse acceptée

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

Connectez-vous pour commenter.

Plus de réponses (1)

Scott MacKenzie
Scott MacKenzie le 27 Juin 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
  1 commentaire
Tyann Hardyn
Tyann Hardyn le 2 Juil 2021
Thank you very much, Sir.... it helped me a lot

Connectez-vous pour commenter.

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by