How to use readtable function with delimiter value.
148 次查看(过去 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.
0 个评论
回答(1 个)
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!