How to use readtable function with delimiter value.

166 次查看(过去 30 天)
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.

回答(1 个)

Walter Roberson
Walter Roberson 2015-6-19
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.

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by