本章节主要介绍了matlab的结构体和cellarrey等重要的类型,然后讲解了一下xlsx与一些基本文件的读写。
详细细节见下面代码:
%%
%structure
student(1).name='John Doe';
student(1).id='jd2@sfu.ca';
student(1).number=301073268;
student(1).grade=[100 75 73;95 91 85.5;100 98 72];
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(1)
%%
%结构体的构造与嵌套
A=struct('data',[3 4 7;8 0 1],'nest',struct('testnum','Test1','xdata',[4 2 8],'ydata',[7 1 6]));
A(1).data
A(1).nest.testnum
A(1).nest.xdata
A(1).nest.ydata
%%
%Cell Array
%Method1
AA(1,1)={[1 4 3;0 5 8;7 2 9]};
AA(1,2)={'Anne Smith'};
AA(2,1)={3+7i};
AA(2,2)={-pi:pi:pi};
AA
AA(1,1)
AA{1,1}
%Method2
AAA{1,1}=[1 4 3;0 5 8;7 2 9];
AAA{1,2}='Anne Smith';
AAA{2,1}=3+7i;
AAA{2,2}=-pi:pi:pi;
AAA
%%
%Array concatenation
%cat()
Ab=[1 2;3 4];
aB=[5 6;7 8];
AB=cat(1,Ab,aB)
AB=cat(2,Ab,aB)
AB=cat(3,Ab,aB)
%%
%File Access
clear;
a=magic(4);
save mydata1.mat
%save mydata2.mat -ascii
%使用上面这条被注释掉的内容能够使普通文本阅读器打开所存的.mat文件
%%
%xlsread
Score=xlsread("04Score.xlsx")
Score=xlsread("04Score.xlsx","B2:D4")
%xlswrite计算平均值
M=mean(Score')';
xlswrite("04Score.xlsx",M,1,'E2:E4');
xlswrite("04Score.xlsx",{'Mean'},1,'E1');
[Score Header]=xlsread('04Score.xlsx')
%%
%fopen&fprintf
x=0:pi/100: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
%%
%fopen&fscanf
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',1);