Borrar filtros
Borrar filtros

Reading a multi line string into a single string

9 visualizaciones (últimos 30 días)
Justin
Justin el 19 de Mzo. de 2011
I have opened a file with a string consisting of three lines. I want to read these lines from the file and store them in my "template" variable as a three line string. All I can get is the last line of the string to be stored in "template"
while feof(fid) == 0;
line = fgets(fid);
template = strvcat(line)
end
Thanks for the help

Respuesta aceptada

Jan
Jan el 19 de Mzo. de 2011
template = {};
while feof(fid) == 0;
template{end + 1} = fgets(fid);
end
templateChar = char(template);
More efficient:
C = textscan(fid, '%s', 'delimiter', '\n');
templateChar = char(C{1});
EDITED: textread -> textscan (Thanks, Jiro!)
  1 comentario
Jiro Doke
Jiro Doke el 20 de Mzo. de 2011
@Jan, I think you meant to use "textscan", not "textread". "textread" requires the first input to be the actual file name, not the output of fopen. And "textscan" is preferred anyway.

Iniciar sesión para comentar.

Más respuestas (1)

Matt Tearle
Matt Tearle el 19 de Mzo. de 2011
You can read the whole contents of a file using fileread. Then use regexp to mess with it -- eg, split on "\n"

Categorías

Más información sobre String Parsing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by