Effacer les filtres
Effacer les filtres

Reading in .txt File

31 vues (au cours des 30 derniers jours)
Isabella
Isabella le 25 Mai 2024
Commenté : Rik le 26 Mai 2024
Hello, I have a text file I'd like to read in. Here is aforementioned text file (text_a2p.txt):
object index_image.nii
plane transverse
range 100 150
factor 3.1415926535897
coeff .0001 .0004 -.0006
As you can see, it's not vertically oriented like most tables (honestly I wouldn't even really call it a table). It also contains both strings and doubles in varying amounts, which throws pretty much every method I've tried completely off-kilter.
The word that begins each line specifies what comes after it (ex. the last line contains the coefficients for a polynomial fit generated by the code). Those specifying words don't actually count as data, and won't end up being processed, they just help to clarify. They could serve as the names of the variables the data is stored in, however.
Unfortunately, the general format of the text file is fixed. It is also worth mentioning that 'object' and 'plane' will always only have one string after them, and 'factor' will always have one number after it, but there could be any number of range pairs and coefficients. So, the text file could also look like this.
object index_image.nii
plane sagittal
range 100 150 200 250
factor 2.7182818284590
coeff 3 1 4 1 5 9
How do I import this text file and neatly separate it so that I can isolate each variable? Ideally, I'd have a few arrays (or cells, or whatever is most convenient) that look like this.
object = "index_image.nii";
plane = "saggital";
range = [100, 150, 200, 250];
factor = 2.17182818284590;
coeff = [3, 1, 4, 1, 5, 9];
The variables don't have to be named any particular way, I'm more just focused on the issue of extracting them. Thank you!
  1 commentaire
Isabella
Isabella le 25 Mai 2024
I've tried tableread, textscan, readcell, I've tried using things like detectImportOptions and tampering with things like ReadVariableNames...the list goes on and on and on. Either it cuts off bits of the table, completely ignores the strings and imports them as NaNs, imports data as headers instead of actual data, crashes and burns...seems MATLAB really doesn't like horizontally oriented tables.

Connectez-vous pour commenter.

Réponse acceptée

Rik
Rik le 25 Mai 2024
Your file is not a table, but is more like a struct. Usually such a file would use a JSON format, but you are not in luck.
What you need to do is to read your file line by line. Then you can select everything up to the first space and use that as the field name. The remainder you should first try to convert to number. If it is a NaN, apparently it is text, so you can keep it as string.
You can use the readlines function to read the file to a string vector, or my readfile function to read the file to a cellstr.
  2 commentaires
Isabella
Isabella le 26 Mai 2024
Modifié(e) : Isabella le 26 Mai 2024
Worked like a charm. Used readlines and then split the array up, converting strings to doubles where necessary. Thank you good sir/ma'am/person!
Rik
Rik le 26 Mai 2024
You're very much welcome.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Text Data Preparation dans Help Center et File Exchange

Produits


Version

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by