Textscan and csv fitness data problem
    4 views (last 30 days)
  
       Show older comments
    
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,:)
0 Comments
Answers (2)
  Walter Roberson
      
      
 on 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 Comments
  Walter Roberson
      
      
 on 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.
  Jeremy Hughes
    
 on 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)
0 Comments
See Also
Categories
				Find more on QSP, PKPD, and Systems Biology in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

