Read a Special Text File
Mostrar comentarios más antiguos
I was wondering if there is a quick way to read the following text file sample where the first data line starts with a bracket. I would like to avoid the hassle of manually removing the bracket from every file I obtain. I use the following command for now after I remove the bracket.
Thanks.
data = textscan(Fopen, '%d %f %d %d %d;', 'HeaderLines',2)
file.txt

1 comentario
Stephen23
el 23 de Abr. de 2022
A simple and very efficient approach would be to simply define the square bracket as whitespace, which you can do using TEXTSCAN's WHITESPACE option. Another good approach would be to use a fixed-width import via READTABLE.
Using STR2NUM is not very efficient.
Respuesta aceptada
Más respuestas (2)
% read file into character vector data
data = fileread('textfile.txt');
% remove the first two lines
idx = find(data == newline(),2);
data(1:idx(end)) = [];
% convert to numeric
M = str2num(data);
% show some results
format long
disp(M(1:10,:));
disp(M(end-9:end,:));
David Hill
el 22 de Abr. de 2022
r=readmatrix('file.txt');%see if this will work
3 comentarios
AEW
el 22 de Abr. de 2022
David Hill
el 22 de Abr. de 2022
If you attach the text file, I would be able to help better.
AEW
el 22 de Abr. de 2022
Categorías
Más información sobre Text Files en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
