convert char to double

10 Ansichten (letzte 30 Tage)
Binu
Binu am 16 Sep. 2015
Kommentiert: Binu am 7 Okt. 2015
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 Kommentar
Walter Roberson
Walter Roberson am 16 Sep. 2015
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?

Melden Sie sich an, um zu kommentieren.

Antworten (4)

Binu
Binu am 5 Okt. 2015
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 Kommentare
Walter Roberson
Walter Roberson am 5 Okt. 2015
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 am 5 Okt. 2015
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

Melden Sie sich an, um zu kommentieren.


Rob Purser
Rob Purser am 2 Okt. 2015
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 am 5 Okt. 2015
Thanks Walter and Rob.I have R2012b.Any more advice suit for this version? Thanks Darshani
  1 Kommentar
Walter Roberson
Walter Roberson am 5 Okt. 2015
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?

Melden Sie sich an, um zu kommentieren.


Walter Roberson
Walter Roberson am 5 Okt. 2015
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 Kommentar
Binu
Binu am 7 Okt. 2015
Great! Thanks Walter

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by