脚本编写-Script Editor
- %%将整个脚本文件分为若干“节”,可以选择“运行当前节”来执行某部分代码,方便debug
- 也可以添加断点debug,此时鼠标悬停于变量上可以查看该变量详细信息
- 若缩进没有对齐,可以选中区域按鼠标右键选择“智能缩进” - %%
- for i=1:10
- x=linspace(0,10,101);
- plot(x,sin(x+i));
- print(gcf,'-deps',strcat('plot',num2str(i),'ps'));
- end
 - 结构化程序设计- 逻辑运算符
- a=10—assigment(赋值) 
- a==10—equal to(判断相等)
Flow Control
if-else-end
if     condition1
       statement1
elseif condition2
       statement2
else 
       statement3
end
switch
switch expression
case   value1
       statement1
case   value2
       statement2
 .   
 .  
 otherwise
       statement
 end
while
while expression
      statement
end
prod(1:n)—n!(n的阶乘)
作业:1+2+3…+999的和
s=0
while n<=999
      s=s+n
end
for
for vaeiable=start:increment:end
    commands
end
为变量预分配空间
Tips
clear all:清除之前所有变量
close all:关闭所有图像
clc:清除页面
;:不执行指令
…+回车:换行号
Ctrl+C:停止运行的指令
自定义函数
练习
Use structured programming to:
1)Copy entries in matrix A to matrix B(将矩阵A元素复制到矩阵B)
2)Change the value in matrix B if their corresponding entries in matrix A is negative.(如果矩阵B中的对应项为负,则更改矩阵B中的值,随便什么数值都可以)
A=[0 -1 4;9 -14 25;-34 49 64]
示例:
A = [0 -1 4 ; 9 -14 25 ; -34 49 64];
B = zeros(3,3);
for i = 1:9
    B(i) = A(i);
end
 disp(A)
 disp(B)
 %将A矩阵复制到B矩阵
 for j = 1:9
     if B(j) < 0
         B(j) = unidrnd(100);
     end
 end
 disp(B)
 %将B矩阵小于0的值,随机赋值一个(0,100)的正整数
A=[0 -1 4;9 -14 25;-34 49 64]
B=[0 0 0;0 0 0;0 0 0] %可以用B = zeros(3,3);
for n=1:9;
    B(n)=A(n);
    n=n+1; %这一行没有必要
    disp(B)
end
for n=1:9;
    if B(n)<0
        B(n)=8
    end
end
作业:
Excise
1)Write a function that asks for a temperature in degrees Fahrenheit( 写一个温度以华氏度为单位的函数,数值由用户提供)
2)Compute the equivalent temperature in degrees Celsius(以摄氏度计算等效温度)
3)Show the converted temperature in degrees Celsius(以摄氏度显示转换温度)
4)The function should keep running until no number is provided to convert(函数应该一直运行,直到没有提供要转换的数字)
5)You may want to use these functions:( 您可能需要使用以下功能:)
input,isempty,break,disp,num2str
示例:
主代码块:
while 1
    F = input ('Temperature in F = ');
    if isempty(F)
        break;
    else
        transform(F);
        disp(['Temperature in C = ',num2str(transform(F))]);
    end
end
功能代码块:
function y = transform(x)
y = (x - 32) * 5 / 9;
自己的:
while 1 %有借鉴示例
    F=input('Temperature in F is=')
    B=isempty(F)
    if B==1 %累赘了
        break
    else
        c=(F-32)/1.8;
        C=num2str(c,2);
        disp(['Temperature in C is=',C])
    end
end
 
                         
                                

