Importing data in secs to matlab
이전 댓글 표시
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?
답변 (2개)
Peter Perkins
2017년 5월 10일
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
2017년 5월 10일
편집: Ryan Simpson
2017년 5월 10일
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.
카테고리
도움말 센터 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!