04变量与档案存取

强制转化同c++

s1 = 'h';

uint(s1);

%求s1的ASC2码

s1 = 'Example';

s2 = 'String';

s3 = [s1 s2]; % ExampleString

s4 = [s1;s2]; % 错误指令,因为两个字符串长度不同

s = 'abcdefa';

'a' == s

每个位置分别做逻辑运算

s('a' == s) = 'Z'

s =ZbcdefZ

s1 = 'abcdefa';

s2 = 'abcdefg';

strcmp(s1,s2)

比较两个字符串是否相等

字符串反转

a = ‘Matlab’
b = fliplr(a)
b = 
'baltaM'
a = 'Matlab'
b = a(end:-1:1)
b = 
'baltaM'

structure

fieldnames(student) 输出结构体含有的字段元胞数组

student = rmfield(student,'id') 从结构体中删除id字段

structure可以进行嵌套

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

%%

%%

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

%%

C = A{1,1} 查看指向什么

D = A(1,1) 查看指向的具体内容

A{1,1}(1,1)

cell array function

cell2struct 将cell转化成struct

a = magic(3)

b = num2cell(a)

mat2cell

c = mat2cell(a,[1 1 1],3)

A = [1 2;3 4]

B = [5 6;7 8]

C = cat(1,A,B)

C = cat(2,A,B)

C = cat(3,A,B)

A和B如何拼接(一维,二维,三维)

若A为r1 * c1的矩阵,则可以将A变成r2 * c2的矩阵,前提是r1 * c1 = r2 * c2

C = reshape(A,r2,c2);

clear;

a = magic(4);

save mydata1.mat

save mydata2.mat -ascii 以ascii码形式将workspace中所有的variable存储到mydata2

load('mydata1.mat')

load('mydata2,mat','-ascii') %读取文件内容

如何存储某个变量?

M = mean(Score')';

xlswrite('04Score.xlsx',M,1,'E2:E4'); %1是指表格1

xlswrite('04Score.xlsx',{'Mean'},1,'E1');%只能读取数字

[Score Header] = xlsread('04Score.xlsx') %将字符串存储到Header中

将数据写回到excel时,先写回去Header,后写Score

  1. generate x,y
  2. open a file
  3. write x,y into the file
  4. close the file

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

Check if it is the end of the file:feof(fid)

fid = fopen('asciiData.txt','r');

i = 1;

while ~feof(fid)

name(i,:) = fscanf(fid,'%5c',1); %读取1个

year(i) = fscanf(fid,'%d',1);

no1(i) = fscanf(fid,'%d',1);

no1(i) = fscanf(fid,'%d',1);

no3(i) = fscanf(fid,'%g',1);

no4(i) = fscanf(fid,'%g',1);

i = i + 1;

end

fclose(fid);

发布了133 篇原创文章 · 获赞 15 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/sgsyacm/article/details/100085620