

We use fprintf function in MATLAB to write our data to a text file.This is because we only had read-only permission for this file and were not able to edit it. Use fopen function to open a file called testfile.Īs we can see in the output, the fprintf function is not able to write the new data to our file.However, in this case, we will not pass ‘w’ as an argument to the fopen function. We will try to edit the same file created in the above example. In this example, we will use the fprintf function to write data to a test file. Please note that, we must pass ‘w’ as an argument to the fopen function, else we will only have the read-only permission to the file and would not be able to write to it or make any changes. X = Īs we can see in the output, the fprintf function has written our data to the file created. Use the type function to confirm if the data is written in the file.

Use ‘%3d’ inside fomatspec to print each value of the array at the required distance.Use the fprintf function to write the input data to this file.Pass the second argument ‘w’ to the fopen function, which signifies that we will be writing to this file Use fopen function to open the file called testfile.Initialize the input data to be written in the file.

The data to be written will be an array of integers. In the next example, we will create a test file and will write data to it.
#Matlab fprintf examples mod#
This is how our input and output will look like in MATLAB:Īs we can see in the output, the fprintf function has the written output of the mod function.
#Matlab fprintf examples code#
Let us now understand the code to use the above 2 functions to write to a file Example #1 fprintf(file_ID, formatSpec, X1, X2, …, Xn) will write the data to our file ‘file_ID’ by applying the formatSpec to all the elements of the input arrays X1, X2, …Xn in the column order.fprintf(formatSpec, X1, X2, …, Xn)will write the data to our file by applying the formatSpec to all the elements of the input arrays X1, X2, …Xn in the column order.
