convert char to table or cell

조회 수: 47 (최근 30일)
Nurfaiz Fathurrahman
Nurfaiz Fathurrahman . 2023년 3월 17일
답변: Stephen23 . 2023년 3월 19일
i have char below separated by '|'
#Network | Station | Latitude | Longitude | Elevation | SiteName | StartTime | EndTime
AU|XMI|-10.4495|105.689499|277.7|Christmas Island Airport, Australia|2007-11-19T00:00:00.0000|2008-10-14T00:00:00.0000
AU|XMI|-10.4501|105.6884|252.0|Christmas Island Airport|2008-10-14T00:00:00.0000|
AU|XMIS|-10.4807|105.651901|243.5|Christmas Island, Australia|2005-08-16T00:00:00.0000|2008-07-29T00:00:00.0000
AU|XMIS|-10.4807|105.652|210.0|Christmas Island Grants Well|2008-07-29T00:00:00.0000|
DW|LEM|-6.8333|107.616699|1252.0|Lembang, Indonesia|1982-06-02T00:00:00.0000|1988-11-06T00:00:00.0000
GE|BKB|-1.2558|116.915497|0.0|GEOFON Station Balikpapan, Kalimantan, Indonesia|2005-12-06T00:00:00.0000|2009-06-08T00:00:00.0000
GE|BKB|-1.1073|116.9048|110.0|GEOFON Station Balikpapan, Kalimantan, Indonesia|2009-06-09T00:00:00.0000|
how to convert to table or cell ?

채택된 답변

Vilém Frynta
Vilém Frynta 2023년 3월 17일
Something like this should work. It would be easier to work with it you attached the file so we can work with it.
Hope I helped.
% Define the format of the data
formatSpec = '%s %s %f %f %f %q %s %s';
% Read the data using textscan with the format you defined
data = textscan(fid, formatSpec, 'Delimiter', '|', 'HeaderLines', 1);
% Convert the cell array to a table
table_data = table(data{1}, data{2}, data{3}, data{4}, data{5}, data{6}, data{7}, data{8}, ...
'VariableNames', {'Network', 'Station', 'Latitude', 'Longitude', 'Elevation', 'SiteName', 'StartTime', 'EndTime'});

추가 답변 (1개)

Stephen23 2023년 3월 19일
T = readtable('data.txt', 'Delimiter','|', 'VariableNamingRule','preserve')
T = 7×8 table
#Network Station Latitude Longitude Elevation SiteName StartTime EndTime ________ ________ ________ _________ _________ ____________________________________________________ _______________________ _______________________ {'AU'} {'XMI' } -10.45 105.69 277.7 {'Christmas Island Airport, Australia' } 2007-11-19T00:00:00.000 2008-10-14T00:00:00.000 {'AU'} {'XMI' } -10.45 105.69 252 {'Christmas Island Airport' } 2008-10-14T00:00:00.000 NaT {'AU'} {'XMIS'} -10.481 105.65 243.5 {'Christmas Island, Australia' } 2005-08-16T00:00:00.000 2008-07-29T00:00:00.000 {'AU'} {'XMIS'} -10.481 105.65 210 {'Christmas Island Grants Well' } 2008-07-29T00:00:00.000 NaT {'DW'} {'LEM' } -6.8333 107.62 1252 {'Lembang, Indonesia' } 1982-06-02T00:00:00.000 1988-11-06T00:00:00.000 {'GE'} {'BKB' } -1.2558 116.92 0 {'GEOFON Station Balikpapan, Kalimantan, Indonesia'} 2005-12-06T00:00:00.000 2009-06-08T00:00:00.000 {'GE'} {'BKB' } -1.1073 116.9 110 {'GEOFON Station Balikpapan, Kalimantan, Indonesia'} 2009-06-09T00:00:00.000 NaT


Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기





Community Treasure Hunt

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

Start Hunting!

Translated by