File Handle
File Handle
Workshop
FILE HANDLE
MATLAB - Data Import
Function & Description:
A = importdata(filename) / Loads data into array A from the file denoted by filename.
A = importdata(filename);
image(A);
When you run the file, MATLAB displays the image file. However, you must store it in the
current directory.
Example 2: Import a Text File and
Specify Delimiter and Column Header
In this example, we import a text file and specify Delimiter and Column Header. Let us create a space-
delimited ASCII file with column headers, named weeklydata.txt.
A = importdata(filename,delimiterIn,headerlinesIn); end
Import a Text File and Return Detected
Delimiter
Using a text editor, create a comma-delimited A=
ASCII file called myfile1.txt.
1,2,3
1 2 3
4,5,6
4 5 6
7,8,9
7 8 9
Import the file, and display the output data and
detected delimiter character.
MATLAB provides the following functions for read and write operations at the byte or character
level −
Function Description
fclose Close one or all open files
feof Test for end-of-file
ferror Information about file I/O errors
fgetl Read line from file, removing newline characters
fgets Read line from file, keeping newline characters
fopen Open file, or obtain information about open files
fprintf Write data to text file
fread Read data from binary file
Move file position indicator to beginning of open
frewind
file
fscanf Read data from text file
fseek Move to specified position in file
ftell Position in open file
fwrite Write data to binary file
Example: Skip to next loop
fid = fopen('magic.m','r’); % open file
count = 0;
continue
end
count = count + 1;
end