Writing and reading a file is often necessary. Like working the real problems on command line is not feasible, same way feeding large amount of input parameter manually is cumbersome and error prone. Generally Matlab/Octave scripts work on processing section of data which are generated from some kind of experiment or other events e.g. image processing. It is not possible for a user to enter value of each pixel manually in a processing algorithm. It is needed to read the image file directly and import data into variable in workspace. In this article we will look into basic file operations.
Saving and Loading Workspace
Many times while working on large projects, one need to stop the processing before picking it up in next session. We don’t want to restart the project from beginning. In such case saving and reloading the environment is the best option. While saving the environment, either complete workspace can be saved or specific variables as needed.
>>save -mat currentworkspace.mat
In above command example, “currentworkspace.mat” is the output file name. User can have any name as per it is sensible. Although it should have “.mat” extension. Saved file would be in the binary format. “save” command with “-mat” flag will save all variables in workspace. If it is required to save just few variables, name of those variables can be added after out-file name.
>>save -mat currentworkspace.mat variable1 variable2
To load all the variables back in the environment “load” command is required.
>>load currentworkspace.mat
If it is preferred to save variables int the text file rather in a binary MATLAB format we can, we can use a, the ASCII format. For this, “-ascii” flag has to be used instead of “-mat” flag. With this only one variable can be saved at a time. If complete workspace is saved, it won’t be able to load the file back.
>> save -ascii currentworkspace.txt
>> load currentworkspace.txt
error: load: unable to determine file format of 'currentworkspace.txt'
>> save -ascii currentworkspace.txt a
>> load currentworkspace.txt
In case of load used as command the workspace will have one variable named same as filename, but contents same as variable a. “load” can also be used as function and by this variable name can be defined by user.
>>a = load('currentworkspace.txt')
Ex: Try saving and reloading a workspace you are working with, using above commands.
Reading and Writing files
If more complex text is required to read or write on a file, ‘fopen‘, ‘fread‘, ‘fgets‘, ‘fprintf‘, ‘fwrite‘ and ‘fclose‘ commands are way to go.
‘fopen‘ function opens the file and returns file ID. normally two input parameters are required for this, [1] filename as string, [2] operation identifier, e.g. ‘r‘ for read, ‘w‘ for write, ‘a‘ for append, ‘rb‘ for reading binary etc.
‘fread‘ function reads the file with specified input parameters. File Id is the required parameter, remaining are optional and can be used as required.
‘fgets‘ function reads the file as string line by line.
‘fprintf‘ function prints any string in the file.
‘fwrite‘ function writes a data in the file without formatting.
‘fclose‘ function closes the file. It is important to close the file after use.
Ex: Get more information for above functions using help command.
Writing
“fprintf” is more commonly used as it writes file in human readable format. for this it requires a format string to be identified. Due to formatting it is slower. with “type” function it can be easily seen what is written in the file.
>> a =[1,2,3,4,5];
>> fileID = fopen('test1.txt', 'w')
fileID = 26
>> fprintf(fileID,"%d\n",a)
>> fclose(fileID)
ans = 0
>> type('test1.txt')
1
2
3
4
5
In the above example “%d\n” is the format string which is specifying to write as integer and one integer in a line. “fwrite” doesn’t do the formatting and write as bytes. So it is needed to identify which kind of data is being supplied to the function.
>> fileID = fopen('test2.txt', 'w')
fileID = 27
>> fwrite(fileID, a, 'uint')
ans = 5
>> fclose(fileID)
ans = 0
>> type('test2.txt')
Here type command return bytes which are unreadable.
Reading
So at this point we have written two files [a] test1.txt which is written using “fprintf “and [b] test2.txt which is written using “fwrite” command. we will read both file with different methods. first we will try “fread” function.
>>fileID = fopen('test1.txt', 'w')
fileID = 26
>>fread(fileID)
ans =
49
10
50
10
51
10
52
10
53
10
>>fclose(fileID)
This file was written using “fprintf“. and “fread” command by default read data as ‘uint8‘ so the output is actually ASCII value.
Ex: Check the ASCII table to verify if these are the correct value. Ex: Typecast the read operation to get output as char.
>> fileID = fopen('test1.txt', 'r')
fileID = 28
>> fgets(fileID)
ans = 1
>> fgets(fileID)
ans = 2
>> fgets(fileID)
ans = 3
>> fgets(fileID)
ans = 4
>> fgets(fileID)
ans = 5
>> fgets(fileID)
ans = -1
>> fclose(fileID)
ans = 0
“fgets” reads line by line as string from the file. Once all lines are read, fgets() return -1.
>> fileID = fopen('test2.txt', 'r')
fileID = 26
>> fread(fileID, 'uint')
ans =
1
2
3
4
5
>> fileID = fopen('test2.txt', 'r')
fileID = 27
>> fread(fileID, 'single')
ans =
1.4013e-45
2.8026e-45
4.2039e-45
5.6052e-45
7.0065e-45
>> fileID = fopen('test2.txt', 'r')
fileID = 28
>> fread(fileID)
ans =
1
0
0
0
2
0
0
0
3
0
0
0
4
0
0
0
5
0
0
0
>> fclose(fileID)
ans = 0
>>
We read the same file with 3 different precision and three different output are received. So it is very important to use correct precision while reading.
Ex: Check dlmread() function to read specific kind of files directly into matrix.
In this article we have looked only in basic file read-write operations. Readers are encouraged to look into documentation for more details. Let us know your query and questions in comment section.
- GNU Octave – Scientific Programming Language
- Octave:1> Getting Started
- Octave:1> Variables and Constants
- Octave:1> Expressions and operators
- Octave:1> Built-In Functions, Commands, and Package
- Octave:2> Vectors and Matrices Basics
- Octave:2> Scripts and Algorithm
- Octave:2> Conditional Execution
- Octave:2> Functions
- Octave:3> File Operations
- Octave:3> Data Visualization