convert char to double
10 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentario
Walter Roberson
el 16 de Sept. de 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?
Respuestas (4)
Binu
el 5 de Oct. de 2015
3 comentarios
Walter Roberson
el 5 de Oct. de 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.
Rob Purser
el 2 de Oct. de 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
0 comentarios
Binu
el 5 de Oct. de 2015
1 comentario
Walter Roberson
el 5 de Oct. de 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?
Walter Roberson
el 5 de Oct. de 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.
Ver también
Categorías
Más información sobre Data Type Conversion en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!