Reading a text file and creating arrays
3 views (last 30 days)
Show older comments
I have a large text file that basically has this format:
# index 13
# P = 0.0005 GPa
85 0.7100 -118.2e-3 1.364e-3 1.408
90 0.7263 -113.2e-3 1.420e-3 1.377
95 0.7438 -108.0e-3 1.477e-3 1.344
100 0.7627 -102.6e-3 1.532e-3 1.311
105 0.7837 -97.0e-3 1.586e-3 1.276
110 41.59 51.1e-3 2.986e-3 0.02404
115 44.05 54.3e-3 3.014e-3 0.02270
120 46.46 57.4e-3 3.040e-3 0.02152
125 48.83 60.4e-3 3.065e-3 0.02048
130 51.16 63.3e-3 3.088e-3 0.01955
...
# index 14
# P = 0.0010 GPa
85 0.7093 -118.0e-3 1.363e-3 1.410
90 0.7255 -113.0e-3 1.419e-3 1.378
95 0.7428 -107.8e-3 1.475e-3 1.346
100 0.7615 -102.4e-3 1.530e-3 1.313
105 0.7822 -96.8e-3 1.584e-3 1.278
110 0.8054 -91.0e-3 1.639e-3 1.242
115 0.8321 -84.9e-3 1.692e-3 1.202
120 21.22 51.5e-3 2.862e-3 0.04713
125 22.61 55.1e-3 2.891e-3 0.04423
130 23.95 58.5e-3 2.918e-3 0.04175
135 25.24 61.9e-3 2.943e-3 0.03962
140 26.50 65.1e-3 2.967e-3 0.03774
145 27.74 68.2e-3 2.989e-3 0.03605
150 28.95 71.3e-3 3.010e-3 0.03454
155 30.15 74.3e-3 3.030e-3 0.03317
...
The indexes go from 13 - 25
How can I create a different aray for each index? Maybe it would an array of arrays? Each array should have 5 columns, corresponding to the data. Note that below each index, there is the pressure. I would like to somehow keep this pressure associated with the data below it. So perhaps it could have it's own array that is somehow indexed to the data.
Currently, I have all of this in a text file. Can MATLAB load this in the way I need? I essentially want to plot Pressure vs. Denisty (last column) with each dataset corresponding to a different temperature (Column 1). Alternatively, I can manually do all this in excel and then load it in MATLAB. Can MATLAB load in this text file directly, and separate out each index?
1 Comment
Guillaume
on 16 May 2019
Can you attach an actual text file to your question (it helps avoid ambiguities on the actual format).
Note that since matlab is a generic programming environment you can do virtually anything you want. The amount of effort however can go from very little to insurmountable.
In your case, the format of the file is unfortunately not ideal. This will force you to write a more complex parser than needed. It would be much better if the data was stored as a continuous array with 6 columns (the first one being the pressure, replicated for each row of each test). Note, that you'll have the same issues importing the data in excel by the way.
Whichever the format of the file, I would strongly recommend storing it as one big array with 6 columns.
Answers (1)
Sulaymon Eshkabilov
on 16 May 2019
Hi Benjamin,
Here is the solution with respect to the given data (Bdata.txt) copied (and one more set just copied) from your given data. Make sure that your data is well formatted in terms of how the headers are set up and empty lines in-between sets, etc (see Bdata.txt).
clearvars
% Specify the file name:
filename='Bdata.txt';
% Define number of rows & columns on each block of numeric data:
Test_rows=15; Test_columns=5;
% Open the file and assign a file ID to it:
fid_test_data=fopen(filename);
% Identify numeric and non-numeric data sets from test results.
% 3 data sets in the above example: Bdata.txt but in your case, Nset = 13 .
Nset = 3;
for n=1:Nset
mydata_test(n).Empty=textscan(fid_test_data, 'headerlines', 1);
mydata_test(n).Index=textscan(fid_test_data, '%13c', 1);
mydata_test(n).P=textscan(fid_test_data, '%17c', 1);
% textscanf() fills the array in column order. Thus, transpose (') the read data:
mydata_test(n).DATA=textscan(fid_test_data,'%d %f %f %f %f',[Test_rows, Test_columns])';
COL1(:,n) = mydata_test(n).DATA{1,1};
COL2(:,n) = mydata_test(n).DATA{2,1};
COL3(:,n) = mydata_test(n).DATA{3,1};
COL4(:,n) = mydata_test(n).DATA{4,1};
COL5(:,n) = mydata_test(n).DATA{5,1};
end
% close the process of data reading/importing with a file ID
fclose(fid_test_data);
Good luck.
1 Comment
Jan
on 17 May 2019
Edited: Jan
on 17 May 2019
+1, a nice and efficient solution.
What is the prupose of clearvars? The best location for this code is a dedicated function. Neitehr this example nor the productive code profit from clearing all formerly existing variables.
A 3D array is smarter than hiding an index in the names of the variables.
Unfortunately the OP did not mention, if all blocks have the same length.
See Also
Categories
Find more on Cell Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!