Read file txt and input value in Matlab
Mostra commenti meno recenti
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?
Risposta accettata
Più risposte (1)
Walter Roberson
il 19 Ott 2015
0 voti
Which MATLAB version are you using? Only quite recently has dlmread() been able to handle text anywhere in the file.
7 Commenti
Walter Roberson
il 19 Ott 2015
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.
ly
il 19 Ott 2015
Walter Roberson
il 19 Ott 2015
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?
ly
il 19 Ott 2015
Walter Roberson
il 19 Ott 2015
Are those input or output?
Categorie
Scopri di più su Large Files and Big Data in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!