In: Electrical Engineering
(MATLAB)
Write a script that would create a 3x5 matrix of random integers and write this new matrix to a file called “Assignment3_Question5.dat”.
MATLAB code:
clc;clear all;
% Let us take range of integers [0,10]
% [0,10] indicates minimum value=0 and maximum value=10
% size 0f matrix [3,5]
disp('The generated matrix is')
M = randi([0,10],[3,5])
% Creating file to be written to
fileName = fopen('Assignment3_Question5.dat','w');
% Writing data to file
fprintf(fileName, '%f %f %f %f %f\n', M.');
% Closing
fclose(fileName);
disp('data stored in Assignment3_Question5.dat is')
%Opening file to read
type Assignment3_Question5.dat
MATLAB result: (in commond window)
The generated matrix is
M =
5 4 8 7 3
7 6 0 7 3
6 3 0 6 9
data stored in Assignment3_Question5.dat is
5.000000 4.000000 8.000000 7.000000 3.000000
7.000000 6.000000 0.000000 7.000000 3.000000
6.000000 3.000000 0.000000 6.000000 9.000000
Screenshot of Assignment3_Question5.dat: