Split Data in Character Array into Matrix

조회 수: 8 (최근 30일)
Vanessa Yau
Vanessa Yau 2020년 5월 24일
댓글: Vanessa Yau 2020년 5월 26일
I currently have a character array (~ 50000 x 1) with data "DD-MM-YY 1000 NaN NaN NaN 0.200 0.300" all in one cell, and I want to split the characters into a matrix with cells for each of the colums (i.e. col1= "DD-MM-YY" col2= 1000, etc.). However, the data in some rows are not perfectly spaced/aligned with other rows because the length of the data may be > than the length of NaN (i.e. "DD-MM-YY 1000 0.111 NaN 0.2002 0.200 0.300 ") so I cannot filter them based on character location. There is extra spacing at the end to account for this shifting (each has a length of 80). Any ideas on how I could split the data, or even how to align all the data columns?
Thanks so much!
  댓글 수: 2
Ameer Hamza
Ameer Hamza 2020년 5월 24일
Can you attach a sample dataset.
Vanessa Yau
Vanessa Yau 2020년 5월 25일
Yes, the text file looks like this:
sample=
' ''May-24-2020 00:00:00" 100.000 NaN NaN NaN 0.3030 '
' ''May-24-2020 00:00:20" 100.0233 NaN 1.4 NaN NaN '
' ''May-24-2020 00:00:40" 100.33155 NaN NaN NaN 0.402 '
' ''May-24-2020 00:01:00" 100.507 NaN NaN NaN 0.7433 '
' ''May-24-2020 00:01:20" 100.900001 NaN NaN NaN 0.224 '

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

채택된 답변

Walter Roberson
Walter Roberson 2020년 5월 25일
temp = regexp( cellstr(sample), '"(?<date>)[^"])"\s+(?<col2>\S+)\s+(?<col3>\S+)\s+(?<col3>\S+)\s+(?<col4>\S+)\s+(?<col5>\S+)\s+(?<col6>\S+)', 'names', 'once');
parts = vertcat(temp{:});
dates = datetime({parts.date}, 'Format', 'MMM-dd-yyyy HH:mm:ss');
col2s = str2double({parts.col2});
col3s = str2double({parts.col3});
col4s = str2double({parts.col4});
col5s = str2double({parts.col5});
col6s = str2double({parts.col6});
  댓글 수: 1
Vanessa Yau
Vanessa Yau 2020년 5월 26일
Ok! I think I got it to work. Thanks!

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

추가 답변 (0개)

카테고리

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