convert char to double

조회 수: 19 (최근 30일)
Binu
Binu 2015년 9월 16일
댓글: Binu 2015년 10월 7일
Hi, I have a data file with strings. It looks like this with 4 columns;
data=19-Jan-2015 11:30:00.000 11.8950011 1.7625011 1.7481285
19-Jan-2015 11:30:01.000 11.8965768 1.7640768 1.7496914
19-Jan-2015 11:30:02.000 11.8952753 1.7627753 1.7484005
i need to separate these columns for my further analysis. i tried y=str2num(data) but it didn't work. Any advice please. Thank you Darshani
  댓글 수: 1
Walter Roberson
Walter Roberson 2015년 9월 16일
Is there a tab delimiter? Visually it appears to be 5 columns, as 19-Jan-2015 would be a different column then 11:30:01.000 if you are using space delimiter.
Do you need the date/time converted to numeric form?
Which MATLAB version do you have? In particular, R2014b or later, or an earlier version?

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

답변 (4개)

Binu
Binu 2015년 10월 5일
Hi Walter, it is 4 columns and the first column has no spaces in it. Yes I need to convert date + time into numeric format. Thank you Darshani
  댓글 수: 3
Walter Roberson
Walter Roberson 2015년 10월 5일
Consider your sample input line
19-Jan-2015 11:30:01.000 11.8965768 1.7640768 1.7496914
This line has 3 clear numeric values, 11.8965768, 1.7640768, and 1.7496914. That is 3 columns. But you also have
19-Jan-2015 11:30:01.000
which contains a space in it. You say that none of your columns have spaces in them, so that implies that one column is 19-Jan-2015 and another column is 11:30:01.000. That's 2 columns. 2 columns plus the 3 clear numeric columns gives 5 columns, not 4.
If you have a day-month-year and a time and you have 4 columns with no spaces, then you could only fit two numeric columns , not 3.
We cannot tell you how to read in the data and convert it until we know exactly what the lines look like.
Binu
Binu 2015년 10월 5일
Hi Walter, Here I attch a part of my data file (same file in .mat and .zip) which would help you see how the data looks like. Thank you Darshani

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


Rob Purser
Rob Purser 2015년 10월 2일
Assuming you're in R2014b or later:
Recreate your data
data = ['19-Jan-2015 11:30:00.000 11.8950011 1.7625011 1.7481285 ' 13 '19-Jan-2015 11:30:01.000 11.8965768 1.7640768 1.7496914' 13 '19-Jan-2015 11:30:02.000 11.8952753 1.7627753 1.7484005']
Parse using textscan
%%Use textscan to parse
C = textscan(data, '%s %s %f %f %f');
% reorganize the cell array
timestamp = strcat(C{1},{' '},C{2});
timestamp = datetime(timestamp,'InputFormat','dd-MMM-yyyy HH:mm:ss.SSS');
parseddata = [C{3} C{4} C{5}];
timestamp
parseddata
Output:
timestamp =
19-Jan-2015 11:30:00
19-Jan-2015 11:30:01
19-Jan-2015 11:30:02
parseddata =
11.8950 1.7625 1.7481
11.8966 1.7641 1.7497
11.8953 1.7628 1.7484

Binu
Binu 2015년 10월 5일
Thanks Walter and Rob.I have R2012b.Any more advice suit for this version? Thanks Darshani
  댓글 수: 1
Walter Roberson
Walter Roberson 2015년 10월 5일
Is the data 5 columns space-separated, or is it 4 columns tab-separated with the first column happening to have a space in it?
Do you need the date + time converted to numeric form?

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


Walter Roberson
Walter Roberson 2015년 10월 5일
lines_cell = regexp(regexprep(Test_data, '\s+$', ''),'\s+', 'split');
lines = vertcat(lines_cell);
numeric_vals = str2double(lines(:,3:end));
dates = datenum(strcat(lines(:,1), {' '}, lines(:,2)), 'dd-mmm-yyyy HH:MM:SS.fff');
Now dates(K) relates to numeric_vals(K,:) . You can break numeric_vals out into separate columns if that is useful.
  댓글 수: 1
Binu
Binu 2015년 10월 7일
Great! Thanks Walter

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

카테고리

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