Download presentation
Presentation is loading. Please wait.
1
Introduction to MATLAB
Session 3 File I/O Graphics Mihaela Duta MSc in Mathematical and Computational Finance Introduction to MATLAB Session 3
2
Today’s topics File I/O Saving and loading the workspace Low-level I/O
Interfaces with various file types Graphics 2D/3D line plots 3D surfaces Figure properties MSc in Mathematical and Computational Finance Introduction to MATLAB Session 3
3
Saving/loading the workspace
When closing a MATLAB session the content of the workspace is lost unless specifilacally saved save/load – saves/loads workspace in/from the file matlab.mat in binary format save/load(<filename>, <var1>,…,<var2>) – saves/loads in/from a specified file <filename> a selection of variables <var1>,…,<var2> Example 1 >> a = 2; b = 3; >> save Saving to: matlab.mat >> clear >> load Loading from: matlab.mat Example 2 >> save('file.mat') >> load('file.mat') Example 3 >> save('file.mat','a') MSc in Mathematical and Computational Finance Introduction to MATLAB Session 3
4
File I/O To read/write information from/in a file we need to
Open the file – establish a data channel Perform data transfer Close the file ASCII files - consist only of alphanumeric chars MSc in Mathematical and Computational Finance Introduction to MATLAB Session 3
5
Opening/closing files
Open files with fopen - returns a file identifier >> fid = fopen(<filename>, <mode>); <mode> - access type ( e.g. 'r', 'w', 'a', etc) Close files with fclose >> fclose(fid); fid = fopen('file.txt') fid = -1 >> fid = fopen('file.txt','w') 3 Check the directory >> fclose(fid); Now we have the file – empty -, we can open it for reading >> fid = fopen('file.txt') MSc in Mathematical and Computational Finance Introduction to MATLAB Session 3
6
Data transfer on ASCII files
Write data in an opened file with fprintf >> count = fprintf(fid,<format_string>,<var>,...) <format>: %f, %d, %e, %s etc general structure Read data from an opened file with fscanf >> [A,count] = fscanf(fid,<format_string>,<size>); Read a line from an opened file with/without newline character with fgets/fgetl >> tline = fgets(fid); >> tline = fgetl(fid); fprintf example a = [ ]; fid = fopen('first_file.dat', 'w'); fprintf(fid, '%e ', a); / fprintf(fid, '%5.2f ', a); fclose(fid) fscanf example fid = fopen('file_to_read.dat', 'r'); [m c] = fscanf(fid, '%f %f %f', [3 Inf]); m = m'; m c fgetl example fid = fopen('file_with_text.txt', 'r'); tline = fgetl(fid); tline while tline ~= -1 end fclose(fid); MSc in Mathematical and Computational Finance Introduction to MATLAB Session 3
7
Various file types csv files
csv files – csvread, csvwrite dlm files - dlmread, dlmwrite textread xls files – xlsfinfo, xlsread, xlswrite MSc in Mathematical and Computational Finance Introduction to MATLAB Session 3
8
Graphical objects Basic elements for displaying graphs
user interface (UI) components Hierarchical structure MSc in Mathematical and Computational Finance Introduction to MATLAB Session 3
9
2D/3D line plots plot(x, y, <line_spec_string>)
<line_spec>: 'r-*', 'b:^', etc (full specs here) plot3(x, y, z, <line_spec>) Logarithmic plots: semilogx/semilogy, loglog 2D >> t = [0:2*pi/20:2*pi]; >> plot(t, sin(t), 'r:*'); 3D >> plot3(t, sin(t), cos(t), 'r:*') Combine plots >> plot(t, sin(t), 'r:*', t, cos(t), 'b-d') MSc in Mathematical and Computational Finance Introduction to MATLAB Session 3
10
Customizing graphs Grids: grid on/off
Control axis: axis(<array_with_axis_limits>) Legend: legend(<legend_matrix>) Label axis: xlabel(<label>), ylabel(<label>), ylabel(<label>) Add title: title(<title_string>) Add text: text(<x_pos>, <y_pos>, <text_str>) Subplots: sublot(<n_rows>, <n_cols>, <card_no>) >> plot(t, sin(t), 'r:*', t, cos(t), 'b-d'); grid on; axis tight; >> xlabel('t'); ylabel('Function'); >> legend(['sin';'cos']); >> title('Plot'); MSc in Mathematical and Computational Finance Introduction to MATLAB Session 3
11
Examples of properties:
Figure properties the handle: gives access to the object’s properties: get(<handle_var>) set(<handle_var>) Examples of properties: children: Children: [0x1 double] colour: Color: [ ] menu bar: MenuBar: 'figure' name: Name: '' number title: NumberTitle: 'on' type: Type: 'figure' MSc in Mathematical and Computational Finance Introduction to MATLAB Session 3
12
3D surfaces For producin a 3D surface we specify the z coordinates above the (x,y) grid Starting from data in arrays(x,y) we create the 2D grid with [X Y] = meshgrid(x, y) 3D graphs: mesh(x,y,z) – wireframe plot surf(z,y,z) – shaded surface pcolor(x,y,z) Utility functions colormap shading colorbar caxis MSc in Mathematical and Computational Finance Introduction to MATLAB Session 3
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.