Is it possible to read a file line by line and then break each line into individual characters/numbers for further processing?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Jay Vaidya
el 3 de Nov. de 2020
Editada: Ameer Hamza
el 3 de Nov. de 2020
If I have a file with the folliwing contents:
a file.dat
a
n pqr 3 2
1 -3 0
2 3 -1 0
Then is it possible to extract the numbers like 3 and 2 when I read the first line? Also, breaking the string in the set of numbers that are separated by spaces except for the pqr. The lines with numbers will be terminated always by '0'. So I would need to get the following result
k = 3
m = 2
j1 = [1 -3]
j2 = [2 3 -1] as the matrix.
Rest all characters are not needed to be read.
0 comentarios
Respuesta aceptada
Ameer Hamza
el 3 de Nov. de 2020
Editada: Ameer Hamza
el 3 de Nov. de 2020
If the txt file follow the same format then try the following code
data = strsplit(fileread('data.txt'), newline);
mn = textscan(data{3}, '%s %s %f %f');
m = mn{3};
n = mn{4};
j1 = sscanf(data{4}, '%f');
j1(end) = [];
j2 = sscanf(data{5}, '%f');
j2(end) = [];
data.txt is attached.
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Get Started with MATLAB 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!