How can I call half of text by using "textread" ? (1*4096 matrix to 1*2048 matrix)
1 次查看(过去 30 天)
显示 更早的评论
How can I call half of text by using "textread" ? (1*4096 matrix to 1*2048 matrix)
1 个评论
dpb
2021-10-16
x=textread('YourFile.dat'); % read the file
x=x(1:fix(size(x,1)/2)); % save first half (will deal with numel(x) odd as well)
采纳的回答
Star Strider
2021-10-16
I have never used textread, however that could be described in the format string, for example —
formatStr = [repmat('%f',1,2048) repmat('%*f',1,2048)];
The addition of the ‘*’ suppresses those fields from being read (assuming they are all numeric). See the doucmentation on format for an extended discussion.
..
1 个评论
dpb
2021-10-16
编辑:dpb
2021-10-17
That works, but
y=textread('YourFile.dat','%f',2048);
is simpler.
I've not compared, but I'd guess the other solution of reading the full file and decimating the unwanted half is probably faster.
"I have never used textread..."
While deprecated, it has its place -- for one thing, it returns the numeric array directly instead of having to cast a cell array with cell2mat as must with textscan. Also it uses the filename directly instead of the need to open and close a file handle.
The advantages have been made less of ones with the recent introduction of readmatrix and friends, however.
The disadvantage of textread is that it does perform more slowly than the later reincarnations as MathWorks has not done anything to improve it; it's around only for the backwards compatibility issues of removing it since it has such a long and storied history.
OTOH, textread has one very significant feature that it will, without an explicit format string being given, return an input file array in the same shape as the file structure; textscan has a penchant for not always doing that or have to provide the precise format string to match the file structure -- which may not always be known a priori.
>> writematrix(rand(256,1),'testfile.dat') % some random data
>> x=textread('testfile.dat'); % read whole thing
>> y=textread('testfile.dat','%f',128); % now read only half
>> whos x y
Name Size Bytes Class Attributes
x 256x1 2048 double
y 128x1 1024 double
>> all(y==x(1:numel(y)),'all') % the same data obtained, just half as much
ans =
logical
1
>>
更多回答(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!