Let open a file and write to it: x = rand (8, 1); fileID = forpend(\'myfile.file
ID: 3838595 • Letter: L
Question
Let open a file and write to it: x = rand (8, 1); fileID = forpend('myfile.file.txt', w);% will create the file fprintf(fileID, '%4.4f ', x); % Write to fileID felose(fileID); % Go to your workspace, look for myfile txt and open it. What do you see? How to read data from an open text file into a column vector A? Use fscanf as: A = fscanf(fileIDformatSpec) x = 5+ rand(10, 1); fileID = fopen (myfile.txt', r);% will create the file fprintf(fileID', %4.4f n', x): % Write to fileID M = fscanf(fileID % f); % Write to fileID % Go to your workspace, look for the vector name M and open it. What do you see? How to move to a specific position in the file? Use Example: Copy 5 bytes from the file test1 dat, starting at the tenth byte, and append to the end of test 2 dat % Create files test 1 dat and test 2 datExplanation / Answer
In both of the examples, you are supposed to run the code given and observe the output.
So, let me explain the code line by line first and then write the output.
(12) The first line is x = rand(8,1);
rand() gives the value between 0 and 1.
rand(8, 1) will give a 8-by-1 matrix i.e. 8 rows and 1 column.
Then, a file named myfile.txt is created and opened in write mode as 'w' is written.
In this file, the value of 'x' is printed using fprintf() and 4.4f means 4 digits before decimal place and 4 digits after decimal places. As the leading number is 0.. 0000.something or 0.something is same so the output I got is...
0.6792
0.3478
0.2234
0.3437
0.6652
0.1947
0.5577
0.9603
The next time I got the following output..
0.3790
0.3772
0.9652
0.0617
0.3154
0.5222
0.8289
0.7356
So, it is randomly printing 8 values to the file.
At last, it is closing the file.
(13).
In this code snippet, you are displayed how to read the values from the file.
In the first line, rand(10,1) means an array of 10-by-1 with value 0 to 1. And when multiply it with 5, it becomes the value between 0 and 4.
In the next line, mytext.txt file is opened in read mode using 'r'.
So, in the next line, fprintf() is used to write in the file but as the file is opened in the read mode, it won't write anything in this file. So, the previous values will be retained there.
In the next line, M is saving the values from the file using fscanf(). In fscanf(), the first argument is the fileID and the second argument is a format specifier in which %f is written which means all the floating point numbers are taken in the vector M.
So, as I said, the new values won't be written in the file, see the first output I wrote in (12). I am getting the same values in M which are as follows....
0.67920
0.34780
0.22340
0.34370
0.66520
0.19470
0.55770
0.96030
For the second output in (12) I got the result as :
0.37900
0.37720
0.96520
0.06170
0.31540
0.52220
0.82890
0.73560
And at the end, the file is closed.
So, these are the explanations for the questions.
Do comment if there is any query. Thank you. :)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.