Textscan and csv fitness data problem

14 vues (au cours des 30 derniers jours)
Thomas Esbensen
Thomas Esbensen le 1 Jan 2018
Challenge: Convert attached csv-file into vectors for timestamps, power, and heart_rate data. The file originates from a FIT-file (from a fitness device) converted via FitSDKRelease into a CSV-file.
Problem: I am not able to generate an output via textscan that corresponds to the number of lines (rows) in the csv-file. E.g. the variable DataMat generated via examples below holds values from various rows in the first line (DataMat(1,:)). It is like some lines wrap around.
Example code
fileID = fopen(PathTargetFile,'rt');
Header = fgets(fileID); %line by line read
%....
LengthHeadder = 126;
StrReadFormat = repmat('%s',1,LengthHeadder);
Data = textscan(fileID, StrReadFormat, 'Delimiter', ',', 'ReturnOnError' , false);
DataMat = cell2mat(Data);
DataMat(1,:)
Another iteration:
fileID = fopen(PathTargetFile,'rt');
Header = fgets(fileID); %line by line read
%....
LengthHeadder = 126;
StrReadFormat = [repmat('%s',1,min(LengthHeadder,40)) '%*[^\n]'];
Data = textscan(fileID, StrReadFormat, 'Delimiter', ',', 'ReturnOnError' , false);
DataMat = cell2mat(Data);
DataMat(1,:)

Réponses (2)

Walter Roberson
Walter Roberson le 1 Jan 2018
Your input has 127 columns, not 126. Column 127 is empty where it exists at all -- but the fact that it exists is throwing off textscan, because textscan always picks up "mid-line" if the format did not use up the entire line.
  4 commentaires
Thomas Esbensen
Thomas Esbensen le 1 Jan 2018
Hmm. I am jumping a bit between MATLAB and Octave. I wasn't aware of this part being very different.
In Octave
fileID = fopen(PathTargetFile,'rt');
LengthHeader = 15;
StrReadFormat = [repmat('%s',1,LengthHeader) '%*[^\n]'];
Data = textscan(fileID, StrReadFormat, 'Delimiter', ',', 'ReturnOnError' , false, "TreatAsEmpty", false);
fclose(fileID);
DataMat = cell2mat(Data); % you can't do this in MATLAB
size(DataMat)
%OUTPUTS 2784 15
fileID = fopen(PathTargetFile,'rt');
LengthHeader = 40;
StrReadFormat = [repmat('%s',1,LengthHeader) '%*[^\n]'];
Data = textscan(fileID, StrReadFormat, 'Delimiter', ',', 'ReturnOnError' , false, "TreatAsEmpty", false);
fclose(fileID);
DataMat = cell2mat(Data); % you can't do this in MATLAB
size(DataMat)
%OUTPUTS 2762 40
fileID = fopen(PathTargetFile,'rt');
LengthHeader = 107;
StrReadFormat = [repmat('%s',1,LengthHeader) '%*[^\n]'];
Data = textscan(fileID, StrReadFormat, 'Delimiter', ',', 'ReturnOnError' , false, "TreatAsEmpty", false);
fclose(fileID);
DataMat = cell2mat(Data); % you can't do this in MATLAB
size(DataMat)
%OUTPUTS 2182 107
In MATLAB, it is consistent to the file length of 2784 rows.
Walter Roberson
Walter Roberson le 1 Jan 2018
I have lost track of what the question is? The code I posted works on MATLAB; the behavior of Octave is mostly off-topic for this forum.

Connectez-vous pour commenter.


Jeremy Hughes
Jeremy Hughes le 1 Jan 2018
Hi Using textscan is great if you have files with varying formats or if you need certain special behaviors.
I found this one pretty straight forward with readtable and import options. Although it picks '/' for the delimiter.
opts = detectImportOptions(filename,'Delimiter',',','TextType','string');
opts.ExtraColumnsRule = 'ignore';
T = readtable(filename,opts);
head(T)

Catégories

En savoir plus sur QSP, PKPD, and Systems Biology dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by