0

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, e.g. Alcohol would be all 11 rows of data. I know Matlab can do this but I can't make it work. Please help.

Gyrocode.com
  • 57,606
  • 14
  • 150
  • 185
Orongo
  • 285
  • 1
  • 6
  • 16

2 Answers2

1

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
Andy Campbell
  • 2,177
  • 13
  • 15
0

You can change your code with this code is given below

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