INTRODUZIONE A MATLAB
-
Upload
sean-fuller -
Category
Documents
-
view
30 -
download
0
description
Transcript of INTRODUZIONE A MATLAB
![Page 1: INTRODUZIONE A MATLAB](https://reader036.fdocumenti.com/reader036/viewer/2022082901/568136d3550346895d9e7194/html5/thumbnails/1.jpg)
Sara Poltronieri
INTRODUZIONE A INTRODUZIONE A MATLABMATLAB
![Page 2: INTRODUZIONE A MATLAB](https://reader036.fdocumenti.com/reader036/viewer/2022082901/568136d3550346895d9e7194/html5/thumbnails/2.jpg)
Archivi
I dati che dovranno essere introdotti in un I dati che dovranno essere introdotti in un programma nonché quelli che sono prodotti programma nonché quelli che sono prodotti da un programma possono essere salvati su da un programma possono essere salvati su degli degli ARCHIVIARCHIVI. .
Le due operazioni fondamentali sono la Le due operazioni fondamentali sono la scritturascrittura e la e la letturalettura..
![Page 3: INTRODUZIONE A MATLAB](https://reader036.fdocumenti.com/reader036/viewer/2022082901/568136d3550346895d9e7194/html5/thumbnails/3.jpg)
Operazioni su files
Vediamo come scrivere dei dati su un archivio di testo. Si voglia salvare la tabulazione della funzione sin(x) entro l’intervallo (1, 2) con passo 0.1.
Si costruisce dapprima il vettore xx = 1 : 0.1 : 2;
e quindi si valuta una matrice MM = [ x; sin(x) ];
![Page 4: INTRODUZIONE A MATLAB](https://reader036.fdocumenti.com/reader036/viewer/2022082901/568136d3550346895d9e7194/html5/thumbnails/4.jpg)
Apertura di un archivio
Decidiamo che il nome esterno dell’archivio su cui si vuole salvare la matrice M sia funzione_sin.txt : esso dovrà essere posto entro apici perché è una stringa.Apriamo l’archivio in scrittura con ’w’ assegnandogli il nome interno archivio_sin.
L’ istruzione è:
archivio_sin = fopen (‘funzione_sin.txt’ , ‘w’);
![Page 5: INTRODUZIONE A MATLAB](https://reader036.fdocumenti.com/reader036/viewer/2022082901/568136d3550346895d9e7194/html5/thumbnails/5.jpg)
fopenfid = fopen(filename,permission)
apre il file filename nella modalità specificata dai permessi(permission). Fid è uno scalare intero chiamato file identifier e lo si usa nel passaggio dei files ad altre routines/functions. Se fopen non riesce ad aprire il file, ritorna il valore –1.I permessi possono essere:‘ r ’ Open file for reading (default).
‘ w’ Open file, or create new file, for writing; discard existing contents, if any.
‘ a’ Open file, or create new file, for writing; append data to the end of the file.
‘ r+’ Open file for reading and writing.‘ w+’ Open file, or create a new file, for reading and writing;
discard existing contents, if any.‘ a+’ Open file, or create new file, for reading and writing;
append data to the end of the file.
![Page 6: INTRODUZIONE A MATLAB](https://reader036.fdocumenti.com/reader036/viewer/2022082901/568136d3550346895d9e7194/html5/thumbnails/6.jpg)
Scrittura di un archivio
Si deposita nell’archivio archivio_sin la matrice M scrivendo le due colonne di numeri decimali:•la prima colonna con 6 cifre di cui 2 decimali•la seconda colonna 8 cifre di cui 4 decimali.Si osservi che fprintf è un acronimo di file print formattedfprintf (archivio_sin ,’%6.2f %8.4f\n’,M);
Si noti che per primo si mette il nome interno, per secondo si mette il formato per terzo si mette l’output da scrivere.Quindi si chiude l’archivio
fclose (archivio_sin)
![Page 7: INTRODUZIONE A MATLAB](https://reader036.fdocumenti.com/reader036/viewer/2022082901/568136d3550346895d9e7194/html5/thumbnails/7.jpg)
fprintffprintf(fid,format,objsToBeWritten)count = fprintf(fid,format,objsToBeWritten)
fprintf formatta i dati contenuti in objsToBeWritten nel formato specificato da format, e li scrive nel file associato con l’identificatore fid ottenuto da fopen.
Count in particolare ritorna il numero di bytes scritti nel file.
![Page 8: INTRODUZIONE A MATLAB](https://reader036.fdocumenti.com/reader036/viewer/2022082901/568136d3550346895d9e7194/html5/thumbnails/8.jpg)
Formato
%-12.5estart of conversionstart of conversion
specificationspecification
flagsflags conversion characterconversion character
precisionprecisionfield widthfield width
![Page 9: INTRODUZIONE A MATLAB](https://reader036.fdocumenti.com/reader036/viewer/2022082901/568136d3550346895d9e7194/html5/thumbnails/9.jpg)
flags
Character Description ExampleCharacter Description Example
A minus sign (-) Left-justifies the converted
argument in its field. %-5.2d
A plus sign (+) Always prints a sign character (+ or -). %+5.2d
Zero (0) Pad with zeros rather than spaces. %05.2d
![Page 10: INTRODUZIONE A MATLAB](https://reader036.fdocumenti.com/reader036/viewer/2022082901/568136d3550346895d9e7194/html5/thumbnails/10.jpg)
Field Width and Precision Specifications
Field widthField width
A digit string specifying the minimum number of digits to be printed.
PrecisionPrecisionA digit string including a period (.) specifying the number of digits to be printed to the right of the decimal point.
![Page 11: INTRODUZIONE A MATLAB](https://reader036.fdocumenti.com/reader036/viewer/2022082901/568136d3550346895d9e7194/html5/thumbnails/11.jpg)
Conversion CharactersSpecifier DescriptionSpecifier Description
%c Single character%d Decimal notation (signed)%e Exponential notation (using a lowercase e as in 3.1415e+00)%E Exponential notation (using an uppercase E as in 3.1415E+00)%f Fixed-point notation%g The more compact of %e or %f. Insignificant zeros do not print.%G Same as %g, but using an uppercase E%i Decimal notation (signed)%o Octal notation (unsigned)%s String of characters%u Decimal notation (unsigned)%x Hexadecimal notation (using lowercase letters a-f)%X Hexadecimal notation (using uppercase letters A-F)
![Page 12: INTRODUZIONE A MATLAB](https://reader036.fdocumenti.com/reader036/viewer/2022082901/568136d3550346895d9e7194/html5/thumbnails/12.jpg)
Escape Characters
fprintf (archivio_sin ,’%6.2g %8.4g\n’,M);
Escape CharacterEscape CharacterCharacter DescriptionCharacter Description\b Backspace\f Form feed\n New line\r Carriage return\t Horizontal tab\\ Backslash\'' or '' (two single quotes) Single quotation mark%% Percent character
![Page 13: INTRODUZIONE A MATLAB](https://reader036.fdocumenti.com/reader036/viewer/2022082901/568136d3550346895d9e7194/html5/thumbnails/13.jpg)
Riassumendo...% scrive archivio
x = 1 : 0.1 : 2;
M = [x; sin(x)];
fid = fopen (’filename.txt’ , ’w’);
fprintf (fid,’formato’,M);
fclose (fid)
Se si vuole vedere l’archivio così creato lo si può fare con un editor di testo oppure digitando edit filename.txt
![Page 14: INTRODUZIONE A MATLAB](https://reader036.fdocumenti.com/reader036/viewer/2022082901/568136d3550346895d9e7194/html5/thumbnails/14.jpg)
Lettura di un archivioSia dato un archivio il cui nome esterno sia del tipo ”archivio.txt” (creato prima). Si apre l’archivio assegnandogli un nome interno:myArchive = fopen ( ‘archivio.txt’ , ‘r’);Il nome del file deve essere posto entro apici perché è una stringa. Si preleva dall’archivio myarchive la matrice M leggendo le due colonne di numeri in formato ”%g”M = fscanf ( myArchive , ‘%f %f’ , [2 inf] );
M = M’;Si noti che per primo si mette il nome interno myarchive, per secondo si mette il formato (’%g %g’), per terzo si mette il numero di elementi per riga (2) e, non sapendo quante righe sono, si mette inf.
il tutto entro parentesi quadre per indicare la matrice da prelevare. Quindi si chiude l’archiviofclose (myarchive);
![Page 15: INTRODUZIONE A MATLAB](https://reader036.fdocumenti.com/reader036/viewer/2022082901/568136d3550346895d9e7194/html5/thumbnails/15.jpg)
fscanffscanf(fid,format,size)
fscanf legge i dati del file identificato dal fid tanti quantine vengono specificati da size nel formato format.
Le opzioni valide per size sono:n Read n elements into a column vector.
inf Read to the end of the file, resulting in a column vector
containing the same number of elements as are in the
file.
[m,n] Read enough elements to fill an m-by-n matrix, filling the matrix in column order. n can be Inf, but not m.
![Page 16: INTRODUZIONE A MATLAB](https://reader036.fdocumenti.com/reader036/viewer/2022082901/568136d3550346895d9e7194/html5/thumbnails/16.jpg)
fwrite & freadIl comando fwritefwrite scrive i dati in un file binario.Il comando freadfread legge i dati contenuti nel file binario.
Sintassi (esempio):M=magic(5);fid = fopen('magic5.bin','w');fwrite(fid,M,'uint32');fclose(fid)
fid = fopen('magic5.bin','r');fread(fid,[5,5],'uint32');fclose(fid)
‘uint32’ = intero, senza segnoa 32 bit
![Page 17: INTRODUZIONE A MATLAB](https://reader036.fdocumenti.com/reader036/viewer/2022082901/568136d3550346895d9e7194/html5/thumbnails/17.jpg)
Save & Load
Il comando save save salva le variabili del WorkSpace su disco.In particolare:
save salva tutte le variabili del WS in un file matlab.matsave filename salva le variabili nel file filename.mat
save filename var1 var2 salva le variabili specificate nelfile filename.mat
Per caricare le variabili di un WS o un intero WS salvatosi usa il comando loadload con la stessa sintassi.