Writ the MATLAB code using \'fopen\', \'fprintf, \'fscanf, and \'fclose\' to cre
ID: 2085579 • Letter: W
Question
Writ the MATLAB code using 'fopen', 'fprintf, 'fscanf, and 'fclose' to create a text file that contains the following (notice the space between the different characters and numbers): x y z 1 4 5 3 6 8 Then, without loosing the contents of the file, append a row of three numbers to the file: 4 7 9 Then, read the contents of the file as follows. First, read the first three characters (i.e., 'x' 'y, 'z') as strings using fscanf and '%s' for formatting, and the remaining as integers using again 'fscanf and '%d' for formatting. Print to the command prompt what is read from the file, using 'fprint', to make sure that it was read properly.Explanation / Answer
Matlab Script:
fileID = fopen('data.txt','a');%file opened in write mode
fprintf(fileID,'%d %d %d ',[4 7 9]);
fclose(fileID);
fileID = fopen('data.txt','r');
A = fscanf(fileID,'%s %s %s',[3 1]);%reads only first row
B = fscanf(fileID,'%d %d %d',[3,3]);%reads only second and third row with size 3
fclose(fileID);
type 'data.txt';
fprintf(A);
fprintf(' ');
command window log:
x y z
1 4 5
3 6 8
4 7 9
xyz
>> B'
ans =
1 4 5
3 6 8
4 7 9
>>
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.