Matlab的语法学习

1、disp(X) 显示变量 参考

创建一个数字变量和一个文本变量。

A = [15 150];
S = 'Hello World.';
显示每个变量的值。

disp(A)
    15   150
disp(S)
Hello World.

2、连接字符串。两种方法,使用[],或者调用strcat函数。 参考

['hello world', 'shine0524', 'abc']; # 将会全部元素拼接
strcat('hello world', 'shine0524');

3、matlab缩进敏感与否问题
不敏感。它类似于C语言那样,基本每行语句都需要加 ; 分号,除了for等语句不加。

4、int2str(num) 将整数转换为字符 参考

chr = int2str(256) % 此时 chr = '256'

5、注释:使用 %

6、退出程序 参考
quit、exit为退出Matlab 这个软件。看看能不能return 退出。

7、遍历数组 参考
(1)元胞数组:类似于c语言里面的结构体,它的元素类型不限制。

方法一:下标遍历

strvec={'i','am','iwantnon'};
str=[];
for i=1:length(strvec)
    str=[str,' ',strvec{i}];
end
disp(str);
结果:i am iwantnon
方法二:元素遍历

strvec={'i','am','iwantnon'};
str=[];
for s=strvec
    str=[str,' ',s{1}];  % 注:s是1*1 cell,要访问之需用s{1}。
end
disp(str);
结果:i am iwantnon

(2)普通数组:

方法一:下标遍历

v=[1,4,6,4,1];
s=0;
for i=1:length(v)
    s=s+v(i);
end
disp(s);

结果:16
方法二、元素遍历

v=[1,4,6,4,1];
s=0;
for ve=v
    s=s+ve;
end
disp(s);
结果: 16

猜你喜欢

转载自blog.csdn.net/qimiejia5584/article/details/81252000