How do I read multiple float values in different columns from a txt-File with headers?

15 vues (au cours des 30 derniers jours)
So I am trying to build an automatic analysis application for specific sets of data and to that extent I want to read the data from files like the one attached. I've tried a lot using fscanf and also textscan, but somehow nothing seems to work. For example,
fid=fopen('728 v FS.txt','r');
fgetl(fid); fgetl(fid); %used to skip the header lines
A=fscanf(fid,['%f',repmat('%*s',1,24), '%[^\n]'],[2,inf]); %here I tried to read only the first column
fclose(fid);
returned not only the first column as row, but also another one that had the constant value 13.000, which isn't even included in the original file. Consequently, when trying again with something like
fid=fopen('728 v FS.txt','r');
fgetl(fid); fgetl(fid); %used to skip the header lines
A=fscanf(fid,['%f %f %f',repmat('%*s',1,22), '%[^\n]'],[2,inf]); %here I tried to read the first three columns
fclose(fid);
the results were not at all as expected. Furthermore, the first code only gives 4 decimals, whereas I want the whole number to be displayed. That is a problem I also have when using textscan, as columns 2 and 3 lose all their decimals and remain as only integer values, while readtable almost gives the desired result, but still cuts the last decimal in those columns.
I am fairly new to Matlab and even though I have done a little C coding a few years back, you would be absolutely right to call me an amateur. So I'd appreciate a solution for reading the whole float values of columns 1, 2, 3 and 7, even more, however, if that came with an explanation of how to deal with similar problems in the future.
Thanks in advance!

Réponse acceptée

Stephen23
Stephen23 le 15 Sep 2020
Modifié(e) : Stephen23 le 15 Sep 2020
You can simply use any of the high-level file importing commands that your version of MATLAB supports, e.g. readtable, readmatrix, and textscan, that easily allow you to specify the columns you want to import:
opt = {'CollectOutput',true, 'HeaderLines',2};
fid = fopen('728 v FS.txt','rt');
fmt = '%f%f%f%*f%*f%*f%f%*[^\n]'; % Import columns 1,2,3,7. Ignore everything else.
out = textscan(fid,fmt,opt{:});
fclose(fid);
giving all of the digits in the original data, from columns 1, 2, 3, and 7:
>> mat = out{1}
mat =
1.000000000000000e-02 1.019500000000000e+04 2.164110000000000e+03 2.122700000000000e-01
1.259000000000000e-02 1.097780000000000e+04 2.285210000000000e+03 2.081700000000000e-01
1.585000000000000e-02 1.161590000000000e+04 2.094810000000000e+03 1.803400000000000e-01
1.995000000000000e-02 1.204810000000000e+04 2.011190000000000e+03 1.669300000000000e-01
2.512000000000000e-02 1.262070000000000e+04 1.994630000000000e+03 1.580500000000000e-01
...
3.162280000000000e+00 1.842680000000000e+04 2.772000000000000e+03 1.504300000000000e-01
3.981080000000000e+00 1.879160000000000e+04 2.975060000000000e+03 1.583200000000000e-01
5.011880000000000e+00 1.919270000000000e+04 3.085440000000000e+03 1.607600000000000e-01
6.309580000000000e+00 1.963870000000000e+04 3.303170000000000e+03 1.682000000000000e-01
7.943290000000000e+00 2.000170000000000e+04 3.500060000000000e+03 1.749900000000000e-01
1.000000000000000e+01 2.047550000000000e+04 3.679730000000000e+03 1.797100000000000e-01
I used format long E to display those values:
  1 commentaire
Jannis Korn
Jannis Korn le 15 Sep 2020
Thanks. I just tried to reinvent the wheel because I wasn't aware how to properly use the existing ones ;)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Import and Export dans Help Center et File Exchange

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by