How to use readtable function with delimiter value.

197 views (last 30 days)
I have uploaded a 35 mb text file into matlab using readtable function.It was supposed to be a 50118*100 matrix. But it becoming 50118*1 where all collumn values are copied into a single cell. i have used a delimiter code
readtable('nnn.txt', 'Delimiter','space');
but it doesnt solve the eissue. i am attaching a snapshot for reference. kindly help.

Answers (1)

Walter Roberson
Walter Roberson on 19 Jun 2015
It looks to me as if there are no spaces between the elements, that the entries are fixed width. I cannot tell whether the "-" are intended as separators or if they are indicating negative values. I cannot tell if all of the values are negative.
My guess at the moment is that you might be able to use 'Delimiter', '-' and then throw away the empty first column (proceeding the first "delimiter" of '-'), and then multiply the entries all by -1. However that is conditional on there being no positive entries.
More generally you might need
numcol = 100;
fmt = repmat('%6c', 1, numcol);
fid = fopen('nnn.txt', 'rt');
datacell = textscan(fid, fmt);
fclose(fid);
data = cellfun(@(C) str2double(cellstr(C)), datacell, 'Uniform', 0);
and then use something that converts the cell array into table().
Note: When you use a %c format, textscan() outputs a character array for that location, rather than a cell array of strings such as would be used for %s. str2double() will not work on character arrays which is why I call cellstr() to convert it to something str2double() can handle.

Categories

Find more on Characters and Strings 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!