###本章内容十分多

第三章:Variables and Data access

Data Types

numeric:double、single、int8(16、32、64bit[integer])、uint8(16、32、64bit[unsigned])


double转成integer:

 A = 20;      %double
 B = intB(A)    %double -->int

Character(char):记录一个字源

 s1 = 'h';   %H
 whos;
 unit16(s1);   %转为ASCLL码

String:字串

 s1 = 'Example'
 s2 = 'String'
 s3 = [s1 s2];    %[]是concatenation;左右组合,中间的space仅仅是吧两个变量分开,并不output
 s4 = [s1;s2];    %上下组合,字节大小要相同,即字数相同

Strcmp:比较字符串

 TF = strcmp(s1,s2)
 %比较s1与s2,如果二者相同,则返回1,反之0.文本大小与内容相同视为相等

Logical Operations

 str = 'aardbark';
 'a' == str       %判断这个数组中a的存在
 str(str == 'a') = 'Z'    %替换str中a为Z

Structure结构体

 student.name = 'Li Tianqi';
 student.id = '741495222@qq.com';
 student.number = 3200812015;
 stduent.grade = [100, 100, 199;...
  95, 95, 92;...
  100, 222, 34];
 student
 %如果要输入第二个student,只需要将student改为student(2);
 %可以使用student(3).grade(3)来output 100;
 %field储存数据

Structure Functions

 cell2struct%将单元格数组转换为结构体数组
 fieldnames%寻找field里的string目录
 getfield
 isfield
 isstruct
 orderfields
 rmfield%删除一个分支
 setfield
 struct%创造一个结构矩阵
 struct2cell
 structfun
 %具体用法自行查询

Nesting Structures(链表套娃XD)

 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';    %一个struct的不同内容
 A(2).nest.xdata = [3 4 2];       %同上,并且这三条记录在一个目录下的不同struct上
 A(2).nest.ydata = [5 0 9];       %不同的定义方式

Cell Array(储存阵列;矩阵套娃;使用{})

 A(1,1) = {[1 4 3; 0 5 8; 7 2 9]};
 A(1,2) = {'Anne Smith'};
 A(2,1) = {3+7i};
 A(2,2) = {-pi:pi:pi};
 ……
 %另一种定义方式
 A{1,1} = [1 4 3; 0 5 8; 7 2 9];
 A{1,2} = 'Anne Smith';
 A{2,1} = 3+7i;
 A{2,2} = -pi:pi:pi;
 ……

Accessing Cell Array

 A(1,1)   %pointer,仅仅查询1,1位置的东西是什么
 A{1,1}   %查询(1,1)位置的元素

Cell Array Functions

 cell
 cell2mat
 cell2struct
 celldisp
 cellfun
 cellplot
 cellstr
 iscell
 mat2cell %将矩阵每一组元素变成单独的矩阵
 num2cell %将矩阵每一个元素变成单独的矩阵
 EG:
  a = magic(3);
  b = num2cell(a);
  c = mat2cell(a,[1 1 1], 3)   %[1 1 1]是行,3是列
 struct2cell

Multidimensional Array(多维矩阵)

 cat()   %将两个东西接起来
 A = [1 2; 3 4];
 B = [5 6; 7 8];
 C = cat(1,A,B); %row--行排序
 C = cat(2,A,B); %column--列排序
 C = cat(3,A,B); %layer--层排序
 %维数的比较容易理解的表示方法
 reshape()
 %改变形状,条件是row1*column1 = row2*column2
 EG:
 A = {'James Bond', [1 2; 3 4; 5 6]; pi; magic(5)}
 C = reshape(A, 1, 4)   %reshape(A, row2, column2)

Checking Variable And Variable Status:检查数据类型

 isinteger
 islogical
 isnan
 isnumeric
 isprime
 isreal
 iscell
 ischar
 isempty
 isequal
 isfloat
 isglobal
 ishadle
 isinf

File Access:如何把Work Space和File System中的数据做交换

 save()  and  load();
  ·a = magic(4);
  save mydata1.mat
  save mydata2.mat -ascii    %-ascii目的是储存的文件能不能用一般的文字浏览器打开,但是第一种方式储存的东西更多
 %work space 储存到 File System
  ·load('mydata1.mat')
  load('mydata2.mat','-ascii')
 %Work space 读取 File System
 %储存指定的变量
  ·save('filename','variables')

Excel File Reading and Writing

 xlsread()
 Score = xlsread('04Score.xlsx')      %仅会读取number部分
 Score = xlsread('04Score.xlse','B2:D4')
 xlswrite()
 M = mean(Score')';
 %mean是对colomn做mean,所以遇到row排列平均时,需要做转置再做mean,输出回去再做转置转成row
 xlswrite('04Score.xlsx', M, 1, 'E2:E4');
 %xlswrite(filename,variable,sheet,location)
 xlswrite('04Score.xlsx', {'Mean'}, 1, 'E1');
 %写入一个字串作为标题
 %练习写入标准差
 [Score Header] = xlsread('04Score.xlsx');
 %将数字部分和文字部分分开处理;Score储存的形式是矩阵;Header储存的形式是cell-将数组部分当作[]
 %练习如何用xlswrite写入标题和内容

Low-level File Input/Output

 fis = fopen('[filename]', '[permission]');   %permission是读写权限;EG:'r','w','a'……
 fprintf(fid, format, x, y, ……); %‘写’指令
 fscanf(fid, format, size);  %size是读取data数
 feof(fid)   %读取完为止
 EG1:
 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));    %5.3f:有5个数位,小数点后有三位(位数包括小数点)
 end
 fclose(fid);
 type sinx.txt
 EG2:
 fid = fopen('asciiData.txt','r');
 i = 1;
 while  ~feof(fid)
  name(i,:) = fscanf(fid,'%5c',1);
  year(i) = fscanf(fid,'%d',1);
  …………
  noN(i) = fscanf(fid,'%g\n');
  i = i+1
 end
 fclose(fid);

最新文章

  1. UIScrollView的delegate方法妙用之让UICollectionView滑动到某个你想要的位置
  2. SQL Pass北京举行2014年第一次线下活动
  3. Shine.js – 帮助你实现各种好看的阴影效果
  4. MVC基础知识 – 2.新语法
  5. Yii源码阅读笔记(二十三)
  6. UIBezierPath 画线
  7. Eclipse或Myeclipse常用快捷键组合详解
  8. [转载]ios入门篇 -hello Word(1)
  9. javascript 笔记(1)
  10. Java 问题定位工具 ——jstack
  11. orcal - 增删改
  12. loadrunner 脚本优化-集合点设置
  13. 现代汽车加入Linux 基金会和 AGL协作平台
  14. spring-boot 速成(7) 集成dubbo
  15. Window修改cmd编码
  16. 基于node-webkit的web项目打包方案
  17. Node.js 连接 MongoDB-7
  18. NLP国内研究方向机构导师
  19. 在 linux 下使用 CMake 构建应用程序
  20. java设计模式-观察者模式,装饰者模式

热门文章

  1. nginx: the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf
  2. 解决Ubuntu下的的“system program problem detected”问题
  3. SQL Server性能优化
  4. 字体样式font-style
  5. mysql两表关联
  6. .NET 7介绍及环境准备
  7. 页面-vue
  8. 解决 使用 params 传递参数 必须 加上 name
  9. python爬虫代码中_获取状态码
  10. [530] C3 Rise Of Darkness Opcodez