Read a Special Text File

9 visualizaciones (últimos 30 días)
AEW
AEW el 22 de Abr. de 2022
Comentada: AEW el 23 de Abr. de 2022
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
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.

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 23 de Abr. de 2022
Editada: Stephen23 el 23 de Abr. de 2022
Here are two much more efficient approaches.
The first is exactly like you are doing now, just adding the WHITESPACE option to efficiently ignore the square bracket:
fnm = 'textfile.txt';
fmt = '%d%f%d%d%d;';
opt = {'HeaderLines',2, 'Whitespace',' \t['};
fid = fopen(fnm,'rt');
data = textscan(fid, fmt, opt{:})
data = 1×5 cell array
{11130×1 int32} {11130×1 double} {11130×1 int32} {11130×1 int32} {11130×1 int32}
fclose(fid);
The second is to import the file as table, which has headers and may be more convenient to access:
opt = detectImportOptions(fnm, 'FileType','fixedwidth', ...
'VariableWidths',[6,9,16,16,16], 'VariableNamesLine',1, ...
'VariableNamingRule','preserve', 'Range',3, 'Whitespace',' [', ...
'ExtraColumnsRule','ignore');
tbl = readtable(fnm,opt)
tbl = 11130×5 table
Sample Time Commanded Pos Encoder 1 Pos Encoder 2 Pos ______ _____ _____________ _____________ _____________ 0 0 0 0 0 1 0.001 0 0 0 2 0.002 0 0 0 3 0.003 0 1 0 4 0.004 0 4 0 5 0.004 0 7 0 6 0.005 0 14 0 7 0.006 0 20 0 8 0.007 0 27 0 9 0.008 0 37 0 10 0.009 0 47 0 11 0.01 0 57 1 12 0.011 0 66 3 13 0.012 0 76 4 14 0.012 0 87 6 15 0.013 0 98 7

Más respuestas (2)

Voss
Voss el 22 de Abr. de 2022
Editada: Voss el 22 de Abr. de 2022
% 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,:));
0 0 0 0 0 1.000000000000000 0.001000000000000 0 0 0 2.000000000000000 0.002000000000000 0 0 0 3.000000000000000 0.003000000000000 0 1.000000000000000 0 4.000000000000000 0.004000000000000 0 4.000000000000000 0 5.000000000000000 0.004000000000000 0 7.000000000000000 0 6.000000000000000 0.005000000000000 0 14.000000000000000 0 7.000000000000000 0.006000000000000 0 20.000000000000000 0 8.000000000000000 0.007000000000000 0 27.000000000000000 0 9.000000000000000 0.008000000000000 0 37.000000000000000 0
disp(M(end-9:end,:));
1.0e+05 * 0.111200000000000 0.000098460000000 0 1.003280000000000 0.222970000000000 0.111210000000000 0.000098470000000 0 1.002280000000000 0.222750000000000 0.111220000000000 0.000098480000000 0 1.001270000000000 0.222530000000000 0.111230000000000 0.000098480000000 0 1.000280000000000 0.222310000000000 0.111240000000000 0.000098490000000 0 0.999280000000000 0.222090000000000 0.111250000000000 0.000098500000000 0 0.998290000000000 0.221870000000000 0.111260000000000 0.000098510000000 0 0.997310000000000 0.221640000000000 0.111270000000000 0.000098520000000 0 0.996330000000000 0.221420000000000 0.111280000000000 0.000098530000000 0 0.995360000000000 0.221190000000000 0.111290000000000 0.000098540000000 0 0.994390000000000 0.220970000000000
  2 comentarios
AEW
AEW el 23 de Abr. de 2022
Thank you!
Voss
Voss el 23 de Abr. de 2022
You're welcome!

Iniciar sesión para comentar.


David Hill
David Hill el 22 de Abr. de 2022
r=readmatrix('file.txt');%see if this will work
  3 comentarios
David Hill
David Hill el 22 de Abr. de 2022
If you attach the text file, I would be able to help better.
AEW
AEW el 22 de Abr. de 2022
Please find it attached.
Thanks.

Iniciar sesión para comentar.

Categorías

Más información sobre Data Import and Export en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by