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 | >> s1 = 'sample'; |
1 | >> s1 = 'a b c d e f g' |
cat()
1 | >> A = [1 2;3 4]; B = [5 6; 7 8]; |
reshape()
1 | >> A = {'james', [1 2;3 4;5 6]; pi, magic(5)} |
M = magic(n) 生成一个n*n的矩阵,矩阵元素是由整数1到n^2组成的并且任何行任何列的和都相等,阶数n必须是大于等于3的标量。
1 | >> a = magic(5) |
File Access
File System <--> Work Space-->
save() and load()
Save (all) workspace data to a file:
1
2
3
4clear;
a = magic(4);
save mydata1.mat
save mydata2.mat -asciiLoad data stored in a file:
1
2load('mydata1.mat')
load('mydata2.mat','-ascii')
Excel File Reading: xlsread()
- Read from Excel spreadsheet
1
2Score = xlsread('04Score.xlsx')
Score = xlsread('04Score.xlsx','B2:D4')
Excel File Writing: xlswrite()
Calculate the means and write into Excel spreadsheet
1
2
3M = 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 | x = 0:pi/10:pi; |
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 | John 1995 12 5 12.3 3.24 |
1 | fid = fopen('asciiData.txt', 'r'); i = 1; |
- name那里的冒号:的意思是对应char数组的每一行