convert char to double

14 ビュー (過去 30 日間)
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 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 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 =
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 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 2015 年 10 月 7 日
Great! Thanks Walter



Help Center および File ExchangeData Type Conversion についてさらに検索


Community Treasure Hunt

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

Start Hunting!

Translated by