Advanced Matlab Course-Experts Vision
Advanced Matlab Course-Experts Vision
Advanced Matlab Course-Experts Vision
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
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.
[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
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