Read file txt and input value in Matlab

Hi,
I want to read txt file and input value in Matlab. I use "dlmread" but it is always error:
"Error using dlmread (line 141) Mismatch between file and format string. Trouble reading number from file (row 1u, field 1u) ==> ITEM: TIMESTEP\n"
This is code:
...
clear
clc
filename = 'posi.txt';
M_initial = dlmread(filename,' ',1,0)
clear filename
...
The 'posi.txt' file was attached.
Anyone can tell what can be done in this case?

2 Commenti

No file was attached.
Sorry, it was attached.

Accedi per commentare.

 Risposta accettata

For some reason, testing for the end of file (with feof) doesn’t work with this file. A for loop and guessing at the number of ‘sections’ in the file will have to do:
fidi = fopen('posi.txt', 'rt');
for k1 = 1:20
data{k1} = textscan(fidi,'%f%f%f%f%f', 'HeaderLines',9, 'Delimiter',' ', 'CollectOutput',1);
fseek(fidi, 1, 0);
end
fclose(fidi);

Più risposte (1)

Walter Roberson
Walter Roberson il 19 Ott 2015
Which MATLAB version are you using? Only quite recently has dlmread() been able to handle text anywhere in the file.

7 Commenti

ly
ly il 19 Ott 2015
Modificato: ly il 19 Ott 2015
R2011b
I try dlmread() with another file, it is working. But when I try with the attached file, it doesn't. I am not sure problem from file or from code.
dlmread() has no chance at all of reading that file, in any version. dlmread() is for files which have the same number of columns on every line, all in the same format, with a particular delimiter between the columns.
You would need to write a custom routine to read those files.
However, if the file was produced by a well-known program it is possible that someone has already written a routine to read the files.
What about the other command? Can they work or not?
And how to write a custom routine to read those files? Can you give some advice?
textscan() can handle sections of it, but otherwise you need fgetl(), check what kind of line you just received, sscanf() to pull apart the line, store the values, go on to the next line.
textscan() is good for the portions where the number of lines that follow in a particular format is fixed (or has been extracted from a previous line.) In some cases where the first field on the line is numeric for all entries in the stretch and the first line that has a different format has text in the first field, then textscan() can also be used to process stretches of unknown length.
I can see from the text headers that you have missing fields in your data; in particular I can see that the atom lines do not have a z coordinate. The text headers that end in "p p p" tend to imply that there should be at least one more value on each of the following two or three lines than are actually present. But to automate this we need to know whether the routine will only ever be used on files that are missing those fields; if not then the missing values detection requires that we read a line at a time for any lines where fields might be missing.
Which program uses these input files?
Ok!
For example, I want to get value in the attached file
from line 10:
"167 1 48.2892 4.92002 -0"
to line 14:
"269 1 48.9993 8.61002 -0"
Which command can be used? Give the code is helpful.
The program uses these input files is LAMMPS.
ly
ly il 19 Ott 2015
Modificato: ly il 19 Ott 2015
They are output. So I need read this txt in Matlab and analyze again.

Accedi per commentare.

Categorie

Richiesto:

ly
il 19 Ott 2015

Risposto:

il 19 Ott 2015

Community Treasure Hunt

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

Start Hunting!

Translated by