arrays - Reading a text file in Matlab and calculating average of values -
i have created text file contains integer values such follows:
1 2 3 4 5 56 10 .. , on the idea find average of these numbers. have done below reason getting multiple outputs:
fid = fopen('random.txt', 'r'); data = fscanf(fid, '%i',1 ); fclose(fid); averagevalues= 'average ' + (sum(data)/length(data))
that call fscanf read first line of file. should place in loop read every line, or use 1 of followings
data = cell2mat(textscan(fid, '%d')); data = dlmread('random.txt') the error in last line. in matlab can't convert doubles strings in way. correct code is
avg = mean(data); disp(['average = ' num2str(avg)]);
Comments
Post a Comment