Reading values from the CSV files using textscan command

5 views (last 30 days)
2015-10-05 22:16:15.896938\tmeasure\t3.02\t3.06\t2.98\t3.43\t2.89\t2.85\t3.41\t3.66\t3.73\t3.43\t2.91\t3.99\t3.35\t3.16\t2.87\t3.51\t3.28\t3.04\t2.75\t2.93\t2.65\t2.68\t2.72\t2.67\t2.89\t2.18\t1.98\t1.92\t1.92\t1.84\t1.61\t2.22\t1.57\t1.98\t1.47\t1.54\t1.54\t1.69\t1.65\t0.95\t0.75\t1.57\t1.75\t1.04\t1.12\t1.42\t1.11\t0.91\t0.23\t0.48\t1.03\t0.94\t0.73\t0.62\t0.32\t0.3\t0.75\t1.11\t1.07\t0.37\t0.71\t0.61\t0.28\t0.2\t0.25\t0.78\t0.53\t0.39\t0.45\t0.5\t0.68\t-0.01\t0.35\t0.5\t0.32\t0.84\t0.42\t0.52\t0.3\t0.29\t0.66\t0.64\t0.22\t-0.12\t0.53\t0.72\t0.44\t-0.25\t0.31\t0.34\t0.47\t0.14\t0.2\t0.03\t0.04\t0.46\t0.87\t0.27\t0.25\t0.53\t0.44\t0.12\t0\t0.25\t0.51\t0.2\t0.5\t0.42\t0.42\t0.13\t-0.05\t0.26\t-0.25\t0.14\t0.72\t0.88\t0.15\t-0.07\t-0.16\t0.53\t0.58\t0.52\t0.73\t0.29\t
I want to split the values and strings into different cell then want to plot. Can anyone kindly help me to solve his issue.
I am using the following comand to put the values in diffierent cells. However for the large values it is really hard for me to use lots of %f. i want to create a loop so that ithe values can goes into the cells.
fid=fopen('full.tsv');
r = textscan(fid,"%s%f%f%f%f",1,"Headerlines",31);

Answers (1)

Walter Roberson
Walter Roberson on 10 Nov 2022
Edited: Walter Roberson on 10 Nov 2022
I recommend that you readtable() with 'Delimiter', '\t' or 'Delimiter', char(9) if it does not recognize '\t'
But otherwise, if you know the number of %f to use, generate the format:
fmt = ['%s', repmat('%f', 1, number_of_fields)];
r = textscan(fid, fmt, 'HeaderLines', 31, 'CollectOutput', 1, 'Delimiter', '\t');
r would then be a 1 x 2 cell array in which r{1} is a cell array of character vectors holding the datetimes, and r{2} is an N x number_of_fields numeric values.
If you use
'%{uuuu-MM-dd HH:mm:ss.SSSSSS}D'
instead of %s then r{1} would be an N x 1 array of datetime values, already converted from text to datetime.
Note: the space inside the %{}D format only works when Delimiter is set to something that does not include space. When Delmiiter includes space, the detection of an input space would be treated as a field delimiter first, rather than the %{}D space having priority. You cannot use textscan() to have %{}D parse spaces in date/time strings unless you have ensured that Delimiter does not include space.

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!