convert char to double

10 visualizzazioni (ultimi 30 giorni)
Binu
Binu il 16 Set 2015
Commentato: Binu il 7 Ott 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 Commento
Walter Roberson
Walter Roberson il 16 Set 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?

Accedi per commentare.

Risposte (4)

Binu
Binu il 5 Ott 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 Commenti
Walter Roberson
Walter Roberson il 5 Ott 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 il 5 Ott 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

Accedi per commentare.


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

Accedi per commentare.


Walter Roberson
Walter Roberson il 5 Ott 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.

Categorie

Scopri di più su Data Type Conversion in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by