Docsity

Programming - Unit 8: External data storage, files and databases (slides), Diapositivas de Programación Informática

Vista previa parcial del texto

¡Descarga Programming - Unit 8: External data storage, files and databases (slides) y más Diapositivas en PDF de Programación Informática solo en Docsity!

U N I T 8. E X T E R N A L

D A T A S T O R A G E :

F I L E S A N D

D A T A B A S E S

1

CONTENTS

  • Introduction
  • Files
    • Text files and binary files
    • Working with Files
    • Saving and loading workspace variables to file
    • Import data
  • Databases

THE NEED FOR EXTERNAL DATA STORAGE

1. Permanent storage of program results
  • Data stored in main memory of the computer is lost when the execution
ends
  • External data storage: hard disk or other storage devices
2. Automatic reading from disk of input data , avoiding manual entry
  • Data in files with a proper format can be read and assigned to the
variables of the program
3. Dealing with data larger than the memory size
  • Data is stored in the hard disk, which has larger storage capabilities, and
it is processed in parts (batches)

TYPES OF EXTERNAL DATA STORAGE

  • Two main ways to store data
    • Files
    • Databases

WHAT IS A FILE?

  • Set of information stored in any medium that can be read by a
computer
  • A file is identified by it's name and the description of the place in the
computer where it's stored (folder's path )
  • File operations
    • > Open file
    • > Read from file
    • > Write to file
    • > Close file

BINARY FILES

  • Data are stored as a sequence of bits corresponding to the binary
representation of variables
  • The file consists of records, each record is a copy of a variable as it is
stored in memory
  • To recover data the user needs to know the variable type
    • You can't just open the file with a text editor, data need to be translated
    • Only the original MatLab program (or a similar one that understands
how data are encoded) will be able to use the data
Binary file
  • Integer, represented with 4 bytes
    • 135 >> 00000000 00000000 00000000 10000111
    • 390 >> 00000000 00000000 00000001 10000110

00000000 00000000 00000000 10000111 00000000 00000000 00000001 10000110

1 st^ value ( 135 ) 2 nd^ value (^390 )

Text file • Integer is converted to characters (digit by digit), each character 2 bytes
  • 135 >> '1' '3' '5' >> ASCII 49, ASCII 51, ASCII 53
  • 00000000 00110001 00000000 00110011 00000000 00110101
  • 390 >> '3' '9' '0' >> ASCII 51, ASCII 57, ASCII 48
  • 00000000 00110011 00000000 00111001 00000000 00110000

00000000 00110001 00000000 00110011 00000000 00110101 00000000 00110011

1 st^ value ( 135 )

SHOULD I USE TEXT OR BINARY FILES?

  • The difference between text and binary files is how they encode data.
  • If you plan to use your data within a MatLab program, binary files are
more efficient
  • Converting data back and forth is time consuming, especially with floats
and doubles
  • Binary files use up less memory
  • Choose text files if you want to share the data
  • Easier to interpret
  • Can be used with other tools (text editors,…)

WORKING WITH FILES

  • Built-in functions for the management of input and output operations
1. Open file: fopen
  • Specify mode: binary/text and read/write
2. Read data
  • fscanf for text files
  • fread for binary files
3. Print data
  • fprintf , for text files (same as fprintf to read from standard IO =keyboard)
  • fwrite for binary files
4. Close file: fclose

EXAMPLE: WRITE TEXT TO TEXT FILE

%% Unit 8. Functions

fileName='myFile.txt'; fid=fopen(fileName,'wt'); % open file in write mode % fid is the file identifier % If fopen cannot open the file, then fileID is -1.

if fid ~= - a=input('Enter first number:\n'); b=input('Enter second number:\n');

fprintf(fid, 'Numbers entered are %i and %i\n', a,b);

result=fclose(fid); % close file end

EXAMPLE WRITE TO BINARY FILE

% binary file fileName='myFile2.bin'; fid=fopen(fileName,'w'); % open binary file in write mode

if fid ~= - fwrite (fid, [a,b]); result=fclose(fid); % close file end

EXAMPLE: READ FROM BINARY FILE

% binary fileName='myFile2.bin'; fid=fopen(fileName,'r'); % open binary file in read mode if fid ~= - matrix=fread (fid); result=fclose(fid); % close file end

fclose

  • status= fclose (fileid)
    • Always close every file you open
    • When writing (or appending) to a file, you can only see the changes in
the file once it is closed

FILE POINTER

  • When we open a file, MatLab defines a pointer in the file to read and write
    • The pointer defines the location on the file where the next read or write operation will take place - Initially the pointer is placed at the beginning of the file - If we open the file in append mode – pointer is placed at the end - When we read/write from/to a file the pointer will move forward accordingly.
  • End of file marker

record 1 record 2 record 3 …

Beginning of file (^) Current position End of file