Reading Specific Columns of File

1 view (last 30 days)
Semih Ufuk Güler
Semih Ufuk Güler on 17 Jan 2021
Commented: Semih Ufuk Güler on 18 Jan 2021
I have a data file like this;
20200001 95 53 66 29
20200002 40 29 9 69
20200003 3 33 45 94
20200004 48 52 79 17
20200005 91 65 76 51
20200006 10 17 30 64
20200007 94 85 66 82
20200008 87 67 14 89
20200009 12 100 51 58
20200010 50 51 49 40
And I need store every column seperatly like this;
student_number(1) = 20200001
student_number(2) = 20200002
exam_1(1) = 95
so far I done This;
fid = fopen('data.txt')
n = textscan(fid,'%*d %*d %*d %*d %*d','delimiter',' ')
But I am not sure it is reading data correctly an how can I acess any data ?

Answers (1)

Walter Roberson
Walter Roberson on 17 Jan 2021
data = readmatrix('TheFileNameGoesHere.txt');
student_number = data(:,1);
exam_1 = data(:,2);
However you should consider instead doing
t = readtable('TheFileNameGoesHere.txt', 'variablenames', {'student_number', 'exam_1', 'exam_2', 'exam_3', 'exam_4'});
after which in your calculations instead of referring to exam_1 you would refer to t.exam_1
mean([t.exam_1, t.exam_2, t.exam_3, t.exam_4], 2)
  5 Comments
Walter Roberson
Walter Roberson on 18 Jan 2021
What shows up for
t = readtable('TheFileNameGoesHere.txt', 'variablenames', {'student_number', 'exam_1', 'exam_2', 'exam_3', 'exam_4'});
class(t)
size(t)

Sign in to comment.

Categories

Find more on Structures 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!