变量与档案存储

MATLAB Data (Variables) Types

Multidimensional Array:

  • logical
  • char
  • numeric: double(default)、int8、uint8、single、int16、uint16、int32、uint32、int64、uint64
  • cell
  • struct

Scalar

  • function handle(@)

Variable(Data) Type Conversion

像function一样使用

  • double()
  • single()
  • int8()
  • int16()
  • int32()
  • int64()
  • uint8()
  • uint16()
  • uint32()
  • uint64()

Logical Operations and Assignments

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
>> s1 = 'sample';
>> s2 = 'abcded';
>> s3 = [s1 s2]

s3 =

'sampleabcded'

>> s4 = [s1; s2]

s4 =

2×6 char 数组

'sample'
'abcded'

>> s3(s3 == 'a') = 'Z'

s3 =

'sZmpleZbcded'

>> s3 == 'e'

ans =

1×12 logical 数组

0 0 0 0 0 1 0 0 0 0 1 0

>>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
>> s1 = 'a b c d e f g'

s1 =

'a b c d e f g'

>> s1(1)

ans =

'a'

>> length(s1)

ans =

13

>> s2 = s1(length(s1):-1:1)

s2 =

'g f e d c b a'

>> s2 = s1(end:-1:1)

s2 =

'g f e d c b a'

>>

cat()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
>> A = [1 2;3 4]; B = [5 6; 7 8];
>> C = cat(1,A,B)

C =

1 2
3 4
5 6
7 8

>> C = cat(2, A, B)

C =

1 2 5 6
3 4 7 8

>> C = cat(3, A, B)

C(:,:,1) =

1 2
3 4


C(:,:,2) =

5 6
7 8

>>

reshape()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
>> A = {'james', [1 2;3 4;5 6]; pi, magic(5)}

A =

2×2 cell 数组

{'james' } {3×2 double}
{[3.1416]} {5×5 double}

>> C = reshape(A, 1, 4)

C =

1×4 cell 数组

列 1 至 3

{'james'} {[3.1416]} {3×2 double}

列 4

{5×5 double}

>> A(4)

ans =

1×1 cell 数组

{5×5 double}

>> A(2)

ans =

1×1 cell 数组

{[3.1416]}

>> A(3)

ans =

1×1 cell 数组

{3×2 double}

M = magic(n) 生成一个n*n的矩阵,矩阵元素是由整数1到n^2组成的并且任何行任何列的和都相等,阶数n必须是大于等于3的标量。

1
2
3
4
5
6
7
8
9
10
11
>> a = magic(5)

a =

17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9

>>

File Access

File System <--> Work Space

save() and load()

  • Save (all) workspace data to a file:

    1
    2
    3
    4
    clear;
    a = magic(4);
    save mydata1.mat
    save mydata2.mat -ascii
  • Load data stored in a file:

    1
    2
    load('mydata1.mat')
    load('mydata2.mat','-ascii')

Excel File Reading: xlsread()

  • Read from Excel spreadsheet
    1
    2
    Score = xlsread('04Score.xlsx')
    Score = xlsread('04Score.xlsx','B2:D4')

Excel File Writing: xlswrite()

  • Calculate the means and write into Excel spreadsheet

    1
    2
    3
    M = mean(Score')';
    xlswrite('04Score.xlsx', M, 1, 'E2:E4');
    xlswrite('04Score.xlsx', {'Mean'}, 1, 'E1');
  • mean是求列平均数的函数,所以要使用两次“转置”

  • 第二个参数是要写进去的东西
  • 第三个参数是excel的第几页
  • 第四个参数是位置

Getting Text in Excel Spreadsheet

  • Getting both the text and numbers

[Score Header] = xlsread('04Score.xlsx')

Low-level File Input/Output

Low-level File I/O Functions

  • fopen
  • fclose
  • fscanf
  • fprintf
  • feof

Open and close a file:

fid = fopen('[filename]', '[permission]');

status = fclose(fid);

permission: ‘r’,’r+’,’w’,’w+’,’a’,’a+’

Writing Sine(正弦) Values into A File

1
2
3
4
5
6
7
8
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

Read and Write through Formatted I/O

  • Read: A = fscanf(fid, format, size);
  • Write: fprintf(fid, format, x, y, …);

Reading from Files

  • Check if it is the end of file: feof(fid)
1
2
3
John 1995 12 5 12.3 3.24
Tom 1995 12 7 2.3 2.0
Jean 1996 3 2 10.2 0
1
2
3
4
5
6
7
8
9
10
11
fid = fopen('asciiData.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);
  • name那里的冒号:的意思是对应char数组的每一行
Donate? comment?