1. 变量类型
logical:0/1
char:单字元 如‘h’ ASCII 0~255 whos查看数据类型
string:多字元 如‘example’
numeric:single.double(默认).int8/16/32/64.uint8/16/32/64
struct:structName.fieldName,如student(n).name;其中name,id,grade等称为fields
rmfiled(student,'id')%去掉student.id这个filed
structure的嵌套,struct array:
s = struct(field1,value1,…,fieldN,valueN)
A = struct('data',[3 4 7;8 0 1], 'nest',...
struct('testnum','Test 1',...
'Xdata',[4 2 8],'ydata',[7 1 6]));
% 包含两个field:
A.data= [3 4 7;8 0 1]
A.nest= struct('testnum','Test 1','Xdata',[4 2 8],'ydata',[7 1 6]);
cell: A(m,n) = {…}; 或者 A{m,n}= …;
A(m,n)是pointer,如果要读内容,用A{m,n}.
如果要访问矩阵的某一特定元素:A{m,n}(i,j)
scalar- function handle@
tip:string connection
s1='example';
s2='string'
s3 = [s1 s2]; %换成s3 = [s1;s2]可吗?为什么
#神奇的#逻辑运算符与赋值:
str = 'aardvark';
str(str =='a') = 'z'
%结果:str的’a'被换成‘z'--zzrdvzark
2.变量类型转换与分割
3. 文件存取
file contents | extension | export function | import function | description |
---|---|---|---|---|
MATLAB formatted data | .mat | save | load | save MATLABworkspace |
Test | save | load | space delimited numbers | |
Spreadsheet(Excel) | .xls .xlsx | xlsread | xlswrite |
save:保存变量到某个文件 save (filename) save (filename,-ascii )
保存特定的变量:save(filename,variables)
load:从文件中引入变量 load(filename) load(filename,‘-ascii’)
Excel File Reading:
xlsread(filename,range)
score = xlsread(‘04score.xlsx’)
score = xlsread(‘04score.xlsx’,‘B2:D4’)
得到表头和数据:[Score Header] = xlsread(filename)
写入数据: xlswrite(filename,A,sheet页码,xlRange);
写入表头:xlswrite(filename,{’header‘},sheet页码,xlRange);
思考:如何一并写入??
Low-level Input/Output:
fopen(filename,’[permission]’)
fprintf(fid,format,size)
fprintf(fid,'%5.3f %8.4f\n',x(i),y(i));
format:%c,%d,%e,%f,%g,%o,%s,%u,%x
fscanf(fid,foemat,size)
fscanf(fid,'%5c',1); % 1表示1bit
记得要fclose(fid)!