Reputation: 243
How do I read the following data into MATLAB matrixes. This following data is stored in a file.
5.1,3.5,1.4,0.2,Point1
4.9,3.0,1.4,0.2,Point2
4.7,3.2,1.3,0.2,Point3
4.6,3.1,1.5,0.2,Point4
5.0,3.6,1.4,0.2,Point5
5.4,3.9,1.7,0.4,Point6
4.6,3.4,1.4,0.3,Point7
5.0,3.4,1.5,0.2,Point8
4.4,2.9,1.4,0.2,Point9
4.9,3.1,1.5,0.1,Point10
5.4,3.7,1.5,0.2,Point11
4.8,3.4,1.6,0.2,Point12
Its four float numbers seperated by commas and followed by a string.
I tried to use the following code but it doesn't seem to work properly.
fid = fopen("file.txt", 'r');
I want to get the four float values in each line into a numberoflines * 4 matrix and the string in each line into a string array.
fid = fopen('iris.data');
tline = fgetl(fid);
while ischar(tline)
disp(tline)
tline = fgetl(fid);
scanf(tline,'%f,%f,%f,%f,%15c');
end
fclose(fid);
I am new to Matlab and I am doing something very wrong here, so please bear with me.
Thanks, Sagar.
Upvotes: 0
Views: 634
Reputation: 2038
try this line:
vec = sscanf( tline( 1:end ), '%f,%f,%f,%f,%*s' )
should work, regards
Upvotes: 2