Matlab binary and text files In this problem, you will write a vector of integer
ID: 3764931 • Letter: M
Question
Matlab binary and text files
In this problem, you will write a vector of integers to both a binary file and a text file. Generate a vector of integers 1 through 100. Open a binary file for writing. Use class uint8 to write this vector to the binary file. Open a text file for writing. Write the vector to the text file using a field width of 3 with a newline after each after each printed number. Use the MATLAB dir function to determine the number of bytes occupied by each of these two files. Print out the number of bytes in the binary file and the number of bytes in the text file.Explanation / Answer
y = linspace(x1,x2)
fileID = fopen('myBinaryFile.bin','w');
fwrite(fileID,y,'uint8');
fclose(fileID);
fileID2 = fopen('myTextFile.txt','w');
fprintf(fileID2,'%3s %12s ',y,'uint8');
fclose(fileID2);
fileInfoText = dir('myTextFile.txt');
fileSize1 = fileInfoText.bytes;
fileInfoBin = dir('myBinaryFile.bin');
fileSize2 = fileInfoBin.bytes;
disp(fileSize1)
disp(fileSize2)