0% found this document useful (0 votes)
6 views11 pages

File Handle

Uploaded by

Souvik Saha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views11 pages

File Handle

Uploaded by

Souvik Saha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

IT

Workshop
FILE HANDLE
MATLAB - Data Import
Function & Description:
 A = importdata(filename) / Loads data into array A from the file denoted by filename.

A = importdata(___, delimiterIn) / Interprets delimiterIn as the column separator in ASCII file,


filename, or the clipboard data. You can use delimiterIn with any of the input arguments in the above
syntaxes.
 A = importdata(___, delimiterIn, headerlinesIn) / Loads data from ASCII file, filename, or the
clipboard, reading numeric data starting from line headerlinesIn+1.
 [A, delimiterOut, headerlinesOut] = importdata(___) / Returns the detected delimiter
character for the input ASCII file in delimiterOut and the detected number of header lines in
headerlinesOut, using any of the input arguments in the previous syntaxes.
Example 1: Import and Display an
Image
Let us load and display an image file. Create a script file and type the
following code in it −
filename = 'ngc6543a.jpg’;

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.

Our text file weeklydata.txt looks like this −

SunDay MonDay TuesDay WednesDay ThursDay FriDay SaturDay

95.01 76.21 61.54 40.57 55.79 70.28 81.53

73.11 45.65 79.19 93.55 75.29 69.87 74.68

60.68 41.85 92.18 91.69 81.32 90.38 74.51

48.60 82.14 73.82 41.03 0.99 67.22 93.18

89.13 44.47 57.63 89.36 13.89 19.88 46.60


Create a script file and type the following code in it − % View data

filename = 'weeklydata.txt'; for k = [1:7]

delimiterIn = ' '; disp(A.colheaders{1, k})

headerlinesIn = 1; disp(A.data(:, k))

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.

filename = 'myfile2.txt’; delimiterOut =


[A,delimiterOut]=importdata(filename) ,
Low-Level File I/O
The importdata function is a high-level function. The low-level file I/O functions in MATLAB
allow the most control over reading or writing data to a file. However, these functions need more
detailed information about your file to work efficiently.

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;

while ~feof(fid) % Test for end of file

line = fgetl(fid); % read line from file, removing newline characters

if isempty(line) || strncmp(line,'%',1) || ~ischar(line)

continue

end
count = count + 1;

end

fclose(fid); % close all open files

You might also like