Reading in .txt File
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 ‘plane’ will always only have one string after it, and ‘factor’ will always have one number after it, but there could be any number of objects, range pairs, and coefficients. So, the text file could also look like this.
object index_image.nii map1_image.nii map2_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", "map1_image.nii", "map2_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!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 ‘plane’ will always only have one string after it, and ‘factor’ will always have one number after it, but there could be any number of objects, range pairs, and coefficients. So, the text file could also look like this.
object index_image.nii map1_image.nii map2_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", "map1_image.nii", "map2_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! 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 ‘plane’ will always only have one string after it, and ‘factor’ will always have one number after it, but there could be any number of objects, range pairs, and coefficients. So, the text file could also look like this.
object index_image.nii map1_image.nii map2_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", "map1_image.nii", "map2_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! text file, data import MATLAB Answers — New Questions