Organising numbers from a text file into a 2 column matrix in MATLAB -
so trying organise .txt file in matlab. file contains width , height of triangles (except first value, number of triangles). numbers organised 2nd value width , 3rd value height of triangle 1, 4th value width , 5th value height of triangle 2,and on. file starts off this.
112
5
.2
1 3
1 2 3 5
54 8
81.4724 16.2182
..........................
so question is. how matlab read file, starting second value, , organise (in case 112 x2) matrix ? also, need specify how organises data ? need fill row 1 row 2 etc.
so far have
triagnlearea = struct(width, height, area) area = 0.5*width*height fileid = fopen('sampletext','r') = fscanf('sampletext',%f)
you have correct. when you're using fopen
, need specify fileid
determines access point of have opened file. specifying filename, not correct.
next, can read of values 1 single array, extract out first element number of triangles want , pull out rest of values. after, use reshape
restructure array 2d matrix desire.
%// open file reading fileid = fopen('sampletext', 'r'); %// read values single array = fscanf(fileid, '%f'); %// close file fclose(fileid); %// pull out total number of triangles numtriangles = a(1); %// @ rest of points , reshape them %// n x 2 array values = reshape(a(2:end), 2, numtriangles).';
here's example given text file. here, you've specified 12 values, there 6 triangles. such, i've made text file looks this:
6 5 .2 1 3 1 2 3 5 54 8 81.4724 16.2182
if put above file called sampletext
, both number of triangles , respective widths , heights:
>> numtriangles numtriangles = 6 >> values values = 5.0000 0.2000 1.0000 3.0000 1.0000 2.0000 3.0000 5.0000 54.0000 8.0000 81.4724 16.2182
you can calculate area of each triangle using product of width , height divided 2:
>> areas = values(:,1).*values(:,2)/2 areas = 0.5000 1.5000 1.0000 7.5000 216.0000 660.6678
Comments
Post a Comment