Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

question on reading txt file

1 visualización (últimos 30 días)
John
John el 17 de Oct. de 2013
Cerrada: MATLAB Answer Bot el 20 de Ag. de 2021
have a following txt file
DATA MATRIX
1 2 3
1 4 5
1 5 7
END
DATA TERMINALS
TP 13 26
TP 128 32
TP 164 45
TP 243 74
TP 499 97
END
EOF
I want to create a matrix M using this data. and a separate array T for the terminals.Please help.
  1 comentario
Cedric
Cedric el 18 de Oct. de 2013
When you say "matrix" for terminals, is it a numeric array with only numbers, or a cell array including 'TP' or whatever code there is at the beginning of each line?

Respuestas (1)

Azzi Abdelmalek
Azzi Abdelmalek el 17 de Oct. de 2013
Editada: Azzi Abdelmalek el 17 de Oct. de 2013
fid = fopen('file.txt');
line1 = fgetl(fid);
res={line1};
while ischar(line1)
line1 = fgetl(fid);
res{end+1} =line1
end
fclose(fid);
res(end)=[]
idx1=find(cellfun(@(x) isequal(x,'DATA MATRIX'),res));
idx2=find(cellfun(@(x) isequal(x,'DATA TERMINALS'),res));
A=res(idx1+1:idx2-2)
B=res(idx2+1:end-1)

La pregunta está cerrada.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by