0% found this document useful (0 votes)
38 views

Advanced Matlab Course-Experts Vision

This document discusses various file handling techniques in MATLAB, including reading and writing Excel files, text files, and .dat files. It also covers the use of structures and cells to store heterogeneous data. Structures allow storing different data attributes together and accessing them by name, while cells can contain mixed data types accessed by index. The document demonstrates fitting data using polynomial regression with the polyfit and polyval functions.

Uploaded by

Hira Mazhar
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)
38 views

Advanced Matlab Course-Experts Vision

This document discusses various file handling techniques in MATLAB, including reading and writing Excel files, text files, and .dat files. It also covers the use of structures and cells to store heterogeneous data. Structures allow storing different data attributes together and accessing them by name, while cells can contain mixed data types accessed by index. The document demonstrates fitting data using polynomial regression with the polyfit and polyval functions.

Uploaded by

Hira Mazhar
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/ 37

LECTURE 5

ADVANCED MATLAB COURSE- EXPERTS


VISION
FILE HANDLING IN MATLAB
 You can read and write multiple file types in
Matlab:
 Excel sheets
 .txt files
 .dat files
 .csv files
 And many more
Reading Excel sheets
Requests
numeric,
 Reading xls sheet…. text and Reads
raw(all) data
just the
numeric
 [num,txt,raw] = xlsread('lec5.xlsx') data
 My_num_matrix=xlsread('sample_file.xls');
Customize what
you want to
 Or by ‘import’ option import and in
which format

 Saving the procedure in a script or a function


Writing Excel sheets
 Xlswrite(‘myfile.xls’, mymatrix)
Reading .dat files
 load('myfile.dat')
Writing .dat files
 dlmwrite('myfile.dat', my_matrix);
Reading .txt file
 Open a file and associate a file identifier with it
 Read it.
 Close it.

 fileID = fopen('my_test_file.txt');
 C = textscan(fileID,'%s %s %f32 %d8 %u %f %f
%s %f');
 fclose(fileID);
Writing text files
 Open a file (if it doesn’t already exist, it will be created) and
associate a file identifier with it.
 Write it.
 Close it.

 my_rand_nos = 100*rand(8,1);
 my_string='Sarah';
 file_ID = fopen('lecture 555 text.txt','w');
 fprintf(file_ID, '%s%f', my_rand_nos, my_string);
 fclose(file_ID);
Format of data fields
Format of data fields
STRUCTURES

 Heterogeneous data of different types and sizes…


how to store it?
 If you want to refer to them by name, use
structures.
Structures
 Suppose you want to store different attributes of
student data, such as name, percentage, and roll
number.
 These different attributes are called ‘fields’.
 ‘.’ dot notation to store values to them.
 Fields can be any kind of data -even another
structure.
Method 1
 student.percentage=91;
 student.roll=23;
 student.name='Kate';

 See workspace now


Method 2
 You can also specify all the field names at once.
 ‘struct’ command.
 Introduce in pairs
 Field name and its corresponding value make a
complete pair

 student=struct('name', 'Kate', 'percentage', 91, 'roll',


23 )
Structures
 A structure is one entity with a number of sub
entities:

Student

Name
Roll
Percentage
Concatenation of structures
 Just like combining matrices
 student1=struct('name', 'Kate', 'percentage', 91,
'roll', 23 )
 student2=struct('name', 'Sam', 'percentage', 89,
'roll', 33 )
 both_students=[student1 student2]
Updating/ modifying values
 both_students(2).roll=90;
To access a particular field of array elements

 both_students.name
 both_students.roll
 both_students.percentage
Exercise
 2 patients
 Names
 Ages
Cells
 If you have heterogeneous data of different sizes
and types, but you would like to access it all as an
array then use ‘cell’ arrays.
 It can have multiple rows and columns just like a
normal matrix- only difference is that the elements
of that matrix can be of many different types and
sizes!
 Use curly brackets to construct a cell array.
Method 1
 Specify elements one by one…

 mycell{1}='Hogwards';
 mycell{2}=[1 2 3];
 mycell{3}=false;
Method 2
 Specify all elements at once.

 mycell={'Hogwarts', [1 2 3], false }

 Xls sheets are imported as cell arrays for most


applications.
 To store lists of text data.
Visualization of a cell structure
 Suppose this is a cell structure of size 2x3
 Just like a matrix- but heterogeneous elements are
the present.
‘Hogwarts’ [1 2 3] false

[1 2 3 4
2356 81 ‘books’
5 3 6 2]
Code 1
 c{1, 1}='hogwarts';
 c{1, 2}=[1 2];
 c{1, 3}=false;
 c{2, 1}=[1 2 3 4
 2356
 5 3 6 2];
 c{2, 2}=81;
 c{2, 3}='books';
Code 2

 mycell={'Hogwarts', [1 2], false; [1 2 3 4; 2 3 5 6;


5 3 6 2], 81, 'buks'};
How to access data
 The 1st element of mycell
How to access data

 Mycell{row, column}to access the data

 Mycell(row, column)to access the particular cell


Which should you use? Cells or structures?

 Ask yourself would you prefer to refer to your variable


elements by number(s) or by name. Then use cell array in
former case and struct array in later. 
 Structure arrays contain data in fields that you access by
name.
 E.g student records are usually stored in a structure array.
 Cell arrays contain data in cells that you access by
numeric indexing
 E.g storing lists of text strings and storing heterogeneous
data from spreadsheets.
Concatenating of strings
Strcat (‘My name is ’ ‘Kevin’)

Strcat(‘myfile’, num2str(4), ‘.txt’)


Three dimensional data
 3D Matrices
3D cells
Data Fitting
 If I tell you that
 x =[ 0 1.3963 2.7925 4.1888 5.5851 6.9813
8.3776 9.7738 11.1701 12.5664]
 Y=[0 0.9848 0.3420 -0.8660 -0.6428 0.6428
0.8660 -0.3420 -0.9848 -0.0000]

 And ask you to give me an n degree equation that


best describes my data………?
 Fit_y=A1 + A2 * x + A3 * x^2 +……+ An * x^(n-1)
Polyfit function
 coefficients = polyfit(x,y,n);

 Where n is the degree of the equation.


 For a 4th degree equation:
 p = polyfit(x,y,4);

 Fitted_Y=A + B*x + C*x^2 + D*x^3


 Where A, B, C and D are the 4 coefficients.
Polyval function

 fittedY =A + B*x + C*x^2 + D*x^3


 This equation can be calculated automatically by
this command:
 fittedY = polyval(coefficients, x);
Matlab Code
 x =[ 0 1.3963 2.7925 4.1888 5.5851 6.9813
8.3776 9.7738 11.1701 12.5664];
 y=[0 0.9848 0.3420 -0.8660 -0.6428 0.6428
0.8660 -0.3420 -0.9848 -0.0000];

 coefficients = polyfit(x,y,7);
 plot(x, y, 'g');
 hold on;
 fittedY = polyval(coefficients, x);
 plot(x, fittedY, 'r')
If n= 5 coefficients
1.5
original y
fitted y
1

0.5

-0.5

-1

-1.5
0 2 4 6 8 10 12 14
If n=7 coefficients
1
original y
0.8 fitted y

0.6

0.4

0.2

-0.2

-0.4

-0.6

-0.8

-1
0 2 4 6 8 10 12 14

You might also like