04data Structure & File Access
04data Structure & File Access
MATLAB IN ENGINEERING
Yan-Fu Kuo Fall 2015
Dept. of Bio-industrial Mechatronics Engineering
National Taiwan University
Today:
• Variables: string, structure, cell
• Data access
Applications of MATLAB in Engineering Y.-F. Kuo 2
Character (char)
• A character is represented in ASCII using a
numeric code between 0 to 255
• Create a character or a string by putting them into
a pair of apostrophe:
s1 = 'h' s2 = 'H'
whos whos
uint16(s1) uint16(s2)
Applications of MATLAB in Engineering Y.-F. Kuo 5
Applications of MATLAB in Engineering Y.-F. Kuo 6
String
• An array collects characters:
s1 = 'Example';
s2 = 'String';
• String concatenation:
s3 = [s1 s2];
s4 = [s1; s2];
Applications of MATLAB in Engineering Y.-F. Kuo 7
• Try this:
Exercise
• Write a script that inverts any given string
Structure
• A method of storing heterogeneous data
• Structures contain arrays called fields
• Student assignment grades:
Structure Functions
cell2struct Convert cell array to structure array
fieldnames Field names of structure, or public fields of object
getfield Field of structure array
isfield Determine whether input is structure array field
isstruct Determine whether input is structure array
orderfields Order fields of structure array
rmfield Remove fields from structure
setfield Assign values to structure array field
struct Create structure array
struct2cell Convert structure to cell array
structfun Apply function to each field of scalar structure
• Try: fieldnames(student)
rmfield(student,'id')
Applications of MATLAB in Engineering Y.-F. Kuo 12
Nesting Structures
Cell Array
• Another method of storing heterogeneous data
• Similar to matrix but each entry contains different
type of data
• Declared using { } A(1,1)={[1 4 3; 0 5 8; 7 2 9]};
A(1,2)={'Anne Smith'};
A(2,1)={3+7i};
1 4 3 A(2,2)={-pi:pi:pi};
0 5 8 'Anne Smith' A
7 2 9
A{1,1}=[1 4 3; 0 5 8; 7 2 9];
A{1,2}='Anne Smith';
3+7i −𝜋 0 𝜋 A{2,1}=3+7i;
A{2,2}=-pi:pi:pi;
A
Applications of MATLAB in Engineering Y.-F. Kuo 14
Exercise
• Create a cell array B that has the following
structure
[5+j*6 4+j*5]
'This is the first cell'
(1x2 complex
(String)
number array)
1 2 3
4 5 6 {'Tim', 'Chris'}
7 8 9 (1X2 string array)
(3x3 integer array)
Applications of MATLAB in Engineering Y.-F. Kuo 16
3+7i −𝜋 0 𝜋
Applications of MATLAB in Engineering Y.-F. Kuo 17
num2cell mat2cell
Applications of MATLAB in Engineering Y.-F. Kuo 19
Multidimensional Array
cat()
• Array concatenation
A=[1 2;3 4]; B=[5 6;7 8];
Multidimensional Array
A{1,1} = [1 2;4 5];
A{1,2} = 'Name';
A{2,1} = 2-4i;
A{2,2} = 7;
B{1,1} = 'Name2';
B{1,2} = 3;
B{2,1} = 0:1:3;
B{2,2} = [4 5]';
C = cat(3, A, B)
Applications of MATLAB in Engineering Y.-F. Kuo 22
reshape()
• Returns a new array with assigned rows and columns
A = [1:3; 4:6];
1 5
1 2 3
𝐴= 𝐵= 4 3
4 5 6
2 6
Applications of MATLAB in Engineering Y.-F. Kuo 23
File Access
File Work
System Space
Pointer
Specifier Description
%c Single character %o Octal notation (unsigned)
%d Decimal notation (signed) %s String of characters
%e Exponential notation %u Decimal notation (unsigned)
%f Fixed-point notation %x Hexadecimal notation
%g The more compact of %e or %f
Applications of MATLAB in Engineering Y.-F. Kuo 33
fid = fopen('04asciiData.txt','r'); i = 1;
while ~feof(fid)
name(i,:) = fscanf(fid,'%5c',1);
year(i) = fscanf(fid,'%d',1);
no1(i) = fscanf(fid,'%d',1);
no2(i) = fscanf(fid,'%d',1);
no3(i) = fscanf(fid,'%g',1);
no4(i) = fscanf(fid,'%g\n');
i=i+1;
end
fclose(fid);
Applications of MATLAB in Engineering Y.-F. Kuo 34
End of Class