档案与变量存取
- Variables : string, structure, cell
- Data access
Variable (Data) Type Conversion
double() | Convert to double precision |
---|
single() | Conver to single precision |
int8() | Convert to 8-bit signed integer |
int16() | Convert to 16-bit signed integer |
int32() | Convert to 32-bit signed integer |
int64() | Convert to 64-bit signed integer |
uint8() | Convert to 8-bit unsigned integer |
uint16() | Convert to 16-bit unsigned integer |
uint32() | Convert to 32-bit unsigned integer |
uint64() | Convert to 64-bit unsigned integer |
>> A=20
A =
20
>> int8(A)
ans =
20
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'
whos
uint16(s1)
s1 =
h
Name Size Bytes Class Attributes
s1 1x1 2 char
ans =
104
>> s1 = 'hex'
s1 =
hex
>> whos
Name Size Bytes Class Attributes
ans 1x1 2 uint16
s1 1x3 6 char
String
- An array collects characters:
s1 = 'Example';
s2 = 'String';
s3 = [s1 s2];
s4 = [s1; s2];
注意 s1与s2的维度不同
在s3后再加一个blank
>> s1 = 'Example';
s2 = 'String ';
s3 = [s1 s2];
s4 = [s1; s2];
>> s3
s3 =
ExampleString
>> s4
s4 =
Example
String
Logical Operations and Assignments
- Many numerical and logical operators can be applied to strings
>> str = 'aardvark';
>> 'a' == str
ans =
1 1 0 0 0 1 0 0
>> str(3)
ans =
r
>> str(str == 'a') = 'z'
str =
zzrdvzrk
Compare the entire string with another?
s1 =
Example
>> flip(s1)
ans =
elpmaxE
Structure
- A method of storing heterogrneous data
- Structures contain arrays called fields
- Student assignment grades:
.name | John Doe |
---|
.id | jdo2@sfu.ca |
.number | 301073268 |
.grade | 100 75 73 95 91 85.5 100 98 72 |
>> student.name = 'John Doe';
student.id = 'jdo2@sfu.ca';
student.number = 301073268;
student.grade = [100,75,73;...
95,91,85.5;...
100,98,72];
student
student =
name: 'John Doe'
id: 'jdo2@sfu.ca'
number: 301073268
grade: [3x3 double]
>> student.grade
ans =
100.0000 75.0000 73.0000
95.0000 91.0000 85.5000
100.0000 98.0000 72.0000
.name | Ann Lane |
---|
.id | aln4@sfu.ca |
.number | 301078853 |
.grade | 95 100 90 95 82 97 100 85 100 |
>> student(2).name = 'Ann Lane';
student(2).id = 'aln4@sfu.ca';
student(2).number = 301078853;
student(2).grade = [95 100 90;95 82 97;100 85 100];
>> student
student =
1x2 struct array with fields:
name
id
number
grade
>> student(1)
ans =
name: 'John Doe'
id: 'jdo2@sfu.ca'
number: 301073268
grade: [3x3 double]
>> student(2)
ans =
name: 'Ann Lane'
id: 'aln4@sfu.ca'
number: 301078853
grade: [3x3 double]
>> student(2).grade(3)
ans =
100
Structure Function | Description |
---|
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 |
% 部分函数用法及运行结果示例
>> fieldnames(student)
ans =
'name'
'id'
'number'
'grade'
>> rmfield(student,'id')
ans =
1x2 struct array with fields:
name
number
grade
Nesting Structures
>> A = struct('data',[3 4 7; 8 0 1],'nest',...
struct('testnum','Test 1',...
'xdata',[4 2 8],'ydata',[7 1 6]));
A(2).data = [9 3 2; 7 6 5];
A(2).nest.testnum = 'Test 2';
A(2).nest.xdata = [3 4 2];
A(2).nest,ydata = [5 0 9];
A.nest
ans =
testnum: 'Test 2'
xdata: [3 4 2]
ans =
testnum: 'Test 1'
xdata: [4 2 8]
ydata: [7 1 6]
ans =
testnum: 'Test 2'
xdata: [3 4 2]
Cell Array
- Another method of storing heterogeneous data
- Similar to matrix but each contains different type of data
- Declared using { }
[ 1 4 3 ; 0 5 8 ; 7 2 9 ] | ‘Anne Smith’ |
---|
3 + 7i | [ -∏ 0 Π ] |
%%
A(1,1) = {[ 1 4 3
A(1,2) = {'Anne Smith'}
A(2,1) = { 3 + 7i }
A(2,2) = { -pi: pi: pi}
A
%%
A{1,1} = [ 1 4 3
A{1,2} = 'Anne Smith'
A{2,1} = 3 + 7i
A{2,2} = -pi: pi: pi
A
---------------------------------------------------------------------
>> A(1,1) = {[ 1 4 3
A(1,2) = {'Anne Smith'}
A(2,1) = { 3 + 7i }
A(2,2) = { -pi: pi: pi}
A
A =
[3x3 double] 'Anne Smith'
[3.0000 + 7.0000i] [1x3 double]
Accessing Cell Array
- Curly braces, { }, are used to access the “content” of cell arrays
>> A(1,1)
ans =
[3x3 double]
>> A{1,1}
ans =
1 4 3
0 5 8
7 2 9
>> A{1,1}(6)
ans =
2
Cell Array Function | Description |
---|
cell | Create cell array |
cellmat | Convert cell array to numeric array |
cell2struct | Convert cell array to structure array |
celldsip | Cell array content |
cellfun | Apply function to each cell in cell array |
cellplot | Graphically display strcture of cell array |
cellstr | Create cell array of strings from character array |
iscell | Determine whether input is cell array |
mat2cell | Convert array to cell array with different sized cells |
num2cell | Convert array to cell array with consistently sized cells |
struct2cell | Convert structure to cell array |
num2cell() & mat2cell()
- Transform a matrix into a cell variable
>> a = magic(3)
b = num2cell(a)
c = mat2cell( a, [ 1 1 1], 3)
a =
8 1 6
3 5 7
4 9 2
b =
[8] [1] [6]
[3] [5] [7]
[4] [9] [2]
c =
[1x3 double]
[1x3 double]
[1x3 double]
Multidimensional Array
Vecrot | Matrix | Array |
---|
row | rows | rows |
column | columns | columns |
| | layers |
- 三维的array 第一个参数是row 第二个是column 第三个是layer
>> A{1,1,1} = [1 2;4 5];
A{1,2,1} = 'name';
A{2,1,1} = 2 - 4i;
A{2,2,1} = 7;
A{1,1,2} = 'Name2';
A{1,2,2} = 3;
A{2,1,2} = 0:1:3;
A{2,2,2} = [4 5]';
>> A
A(:,:,1) =
[2x2 double] 'name'
[2.0000 - 4.0000i] [ 7]
A(:,:,2) =
'Name2' [ 3]
[1x4 double] [2x1 double]
cat()
>> A = [1 2;3 4];
B = [5 6;7 8];
C = cat(1,A,B); % row
D = cat(2,A,B); % column
E = cat(3,A,B); % layer
>> C
C =
1 2
3 4
5 6
7 8
>> D
D =
1 2 5 6
3 4 7 8
>> E
E(:,:,1) =
1 2
3 4
E(:,:,2) =
5 6
7 8
利用cat(),A 和 B 建立 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)
C(:,:,1) =
[2x2 double] 'Name'
[2.0000 - 4.0000i] [ 7]
C(:,:,2) =
'Name2' [ 3]
[1x4 double] [2x1 double]
reshape() : (r1,c1)–>(r2,c2)
- Return a new array with assigned rows and columns
>> A = {'James Bond',[1 2;3 4;5 6]; pi,magic(5)} % A:2×2
C = reshape(A,1,4) % C:1×4
A =
'James Bond' [3x2 double]
[ 3.1416] [5x5 double]
C =
'James Bond' [3.1416] [3x2 double] [5x5 double]
- Exercise : create a matrix B from the martix A below using reshape:
>> A = [1:3
A =
1 2 3
4 5 6
>> B = reshape(A,3,2)
B =
1 5
4 3
2 6
Checking Variable And Variable Status
isFunction | Description |
---|
isinteger | Determine if input is integer array |
islogical | Determine if input is logical array |
isnan | Detect an element that isnot a number(NaN) |
isnumeric | Determine if input is numeric array |
isprime | Detect prime elements of array |
isreal | Determine if all array elements are real numbers |
iscell | Determine if input is cell array |
ischar | Determine if input is character arrar |
inempty | Determine if input is empty array |
isequal | Determine if arrays are numerically equal |
isfloat | Determine if input is floating-point array |
isglobal | Determine if input is global variable |
ishandle | Detect valid graphics object handles |
ininf | Determine infinite elements of array |
File Access
- File System <–> Work Space
- Supported file formats:
File Content | Extension | Description | Import Function | Export Function |
---|
MATLAB formatted data | MAT | Saved MATLAB workspace | load | save |
Text | | Space delimited numbers | load | save |
Spreadsheet | XLS,XLSX | EXCEL | xlsread | xlswrite |
save() & load()
- Save (all) workspace data to a file:
clear
save mydata1.mat % 将 workspace 所有变量存储在 mydata1.mat
save mydata2.mat -ascii % 可以用记事本打开,即可阅读,纯文本
- Load data stored in a file:
load('mydata1.mat')
load('mydata2.mat','-ascii')
Save a specific variable:
save mydata1.mat x % 只储存变量 x
Excel File Reading : xlsread()
- Read from Excel spreadsheet
Score = xlsread('04Score.xlsx')
Score = xlsread('04Score.xlsx','B2:D4')
Excel File Writing : xlswrite()
- Calculate the means and write into Excel spreadsheet
M = mean(Score')'; % ' 意为 转置
xlswrite('04Score.xlsx',M,1,'E2:E4');
xlswrite('04Score.xlsx',{'Mean'},1,'E1');
Getting Text in Excel Spreadsheet
- Getting both the tet and numbers
[Score Header] = xlsread('04Score.xlsx')
% [numeric string] header 是用 cell 进行存储的
- Read and write file at the byte or character level
- A file has an ID fid
- Location in the a specifiedd by a pointer that can be moved around
Function | Descripyion |
---|
fopen | Open file, or obtain information about open files |
fcolse | Close one or all open files |
fscanf | Read data from text file |
fprintf | Write data to text file |
feof | Test for end-of-file |
- Open and close a file:
fid = fopen('[filename]','[permission]');
status = fclose(fid);
- Exsecise : write sine values into a file
x = 0:pi/10:pi ;
y = sin(x);
fid = fopen('sinx.txt','w');
for i = 1:11
fprintf(fid,'%5.3f %8.4f\n',x(i),y(i));
end
fclose(fid);
type sinx.txt