Importing data in secs to matlab
Mostrar comentarios más antiguos
I used Matlab to extract response times from an array of sensors, which I inserted into a table as follows:
Line 1: Concentration, Sensor 1, Sensor 2, Sensor 3;
Line 2: 1,2.3 secs, 3.2 secs, 3.4 secs;
Line 3:3,2.3 secs, 3.2 secs, 3.4 secs;
How do I get rid of the secs and just display a number? Furthermore, how do I import data of this format (i.e. 4 secs) back into matlab?
Respuestas (2)
Peter Perkins
el 10 de Mayo de 2017
Simon, there's not currently a way to use readtable to create a duration array directly from a file. If you are the one creating the csv file, and you're doing it using writetable, perhaps you could call the seconds function like this
t.Sensor1 = seconds(t.Sensor1);
and then write out the modified table. Then when reading it back in, reverse the process (using exactly the same command -- seconds converts both ways).
If you are not the one creating the file, but the format is consistent, you could read them as strings, strip off the 'secs' part using strrep, and then convert to numeric using str2double.
Ryan Simpson
el 10 de Mayo de 2017
Editada: Ryan Simpson
el 10 de Mayo de 2017
Simon,
Here is what I was able to come up with, please note that I didn't include any handling for NaNs or missing values.
% Path to your file
filename = 'test.txt';
% Open the file
fileID = fopen(filename);
% Read in the header text
header = textscan(fileID,'%s',4,'Delimiter',',');
% Read in the data
data = textscan(fileID,'%d %f secs %f secs %f secs;','Delimiter',[',']);
% Close the file
fclose(fileID);
% Format the header so it will work with variable names
header = header{1};
header = strrep(header,' ','');
header = strrep(header,';','');
% Create a new table to store data
SensorData = table(data{1},data{2},data{3},data{4},'VariableNames',header);
% If you want to store data as seconds replace the line above with the
% following
%SensorData = table(data{1},seconds(data{2}),seconds(data{3}),seconds(data{4}));
And you don't have to use tables but I find them very handy.
Categorías
Más información sobre Text Data Preparation en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!