Read string and numeric array in CSV file

6 views (last 30 days)
Hello,
I am trying to automatize the post-processing of csv files such as the one enclosed here.
I want to extract :
  • the value of Hresolution
  • the four columns of data
I have tried to use readtable but I can not figure out which options to select.
Thanks for your help

Answers (1)

Cris LaPierre
Cris LaPierre on 25 Jul 2022
If you save your file as a 'xlsx' file, you could take advantage of the 'Range' option in readtable.
Without it, you may have to be a little more creative. The approach I took was to disable a lot of the autodetection readtable uses. This approach means your files must all have the same format. Some of the 'header' values look incorrect because this approach treats them all as numeric values. Those that aren't get read in as NaN.
file = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/1076420/LDB100B.CSV';
data = readtable(file,'ReadRowNames',true,'ExpectedNumVariables',5,'PartialFieldRule','fill','numheaderlines',0)
data = 41970×4 table
Var2 Var3 Var4 Var5 _________ __________ ______ ______ Header Size 15 NaN NaN NaN Model Name NaN NaN NaN NaN Comment NaN NaN NaN NaN BlockNumber 1 NaN NaN NaN TraceName NaN NaN NaN NaN BlockSize 41955 41955 41955 41955 VUnit NaN NaN NaN NaN SampleRate 5 5 5 5 HResolution 0.2 0.2 0.2 0.2 HOffset -18018 -18018 -18018 -18018 HUnit NaN NaN NaN NaN DisplayPointNo. 1 1 1 1 PhaseShift 1 1 1 1 Date NaN NaN NaN NaN Time NaN NaN NaN NaN Row16 0.0001725 0.00017167 1.6 1.6417
hRes = data{'HResolution',1}
hRes = 0.2000
vals = data{(data{'Header Size',1}+1):end,:}
vals = 41955×4
0.0002 0.0002 1.6000 1.6417 0.0002 0.0002 1.6000 1.6417 0.0002 0.0002 1.6000 1.6417 0.0002 0.0002 1.6000 1.6417 0.0002 0.0002 1.6000 1.6417 0.0002 0.0002 1.6000 1.6417 0.0002 0.0002 1.6000 1.6417 0.0002 0.0002 1.6000 1.6417 0.0002 0.0002 1.6000 1.6417 0.0002 0.0002 1.5996 1.6417

Categories

Find more on Cell Arrays in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!