Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

MATLAB question: A file \"floatnums.dat\" has been created for use in an experim

ID: 640436 • Letter: M

Question

MATLAB question:

A file "floatnums.dat" has been created for use in an experiment. However it contains float(real) numbers and what is desired is integers. also, the fie is not exactly in the correct format; the values are stored columnwise rather than rowwise. for example, if the file contains the following:

90.5792 27.8498 97.0593

12.6987 54.6882 95.7167

91.3376 95.7507 48.5376

63.2359 96.4889 80.0280

9.7540 15.7613 14.1886

what is really desired is:

91 13 91 63 10

28 55 96 96 16

97 96 49 80 14

create the data file in the specified format. write a script that would read from the file floatnums.dat into a matrix, round the numbers, and write the matrix in the desired format to a new file called intnums.dat,

Explanation / Answer

fileID = fopen('float.txt','w');
formatSpec = '%f %d';
sizeA = [3 Inf];
A = fscanf(fileID,formatSpec,sizeA)
fprintf([repmat('%d ', 1, size(A, 2)) ' '], A');
fclose(fileID);