简体   繁体   中英

Reading .txt. data in Matlab

I have a very basic table

Alcohol  Tobacco
6.47    4.03
6.13    3.76
6.19    3.77
4.89    3.34
5.63    3.47
4.52    2.92
5.89    3.20
4.79    2.71
5.27    3.53
6.08    4.51
4.02    4.56

I have tried reading it in using textscan but get blank.

fileID = fopen('TabaccoAlcohol.txt');
C_text = textscan(fileID,'%n',2);

It would be nice in the program using the headings as objects, eg Alcohol would be all 11 rows of data. I know Matlab can do this but I can't make it work. Please help.

Use readtable :

>> t = readtable('data.txt')

t = 

    Alcohol    Tobacco
    _______    _______

    6.47       4.03   
    6.13       3.76   
    6.19       3.77   
    4.89       3.34   
    5.63       3.47   
    4.52       2.92   
    5.89        3.2   
    4.79       2.71   
    5.27       3.53   
    6.08       4.51   
    4.02       4.56  

>> t.Alcohol

ans =

    6.4700
    6.1300
    6.1900
    4.8900
    5.6300
    4.5200
    5.8900
    4.7900
    5.2700
    6.0800
    4.0200

You can change your code with this code is given below

fileID = fopen('read.txt');
C_text = textscan(fileID,' %f %f');
fclose(fileID);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM