matlab常用函数系列之六return&global

1、return

matlab程序中,执行到return 语句之后,即程序返回到调用层面;

当return存在于if、for、while等条件或循环语句,在return条件成立时,将会退出循环或条件语句!

a = 1;                  % 定义一个变量a
flag = 1;              % 定义一个标准量 
if 1
    if flag == a       %判断a与flag是否相等 
        disp('0');    %如果相等,命令行打印0
        return;       %不再向下执行 
        disp('1');    %return 后的语句不执行
    else 
        disp('2');    %如果不相等,命令行打印2
    end
else
    disp('3');        %外层if对应的else,命令行打印3,
end
disp('4');            %命令行打印4

matlab官方自带example:

% At the command prompt, call the function.
A = [3 7 28 14 42 9 0];
b = 81;
findSqrRootIndex(b,A)

子函数:

function idx = findSqrRootIndex(target,arrayToSearch)

idx = NaN;
if target < 0
   return
end

for idx = 1:length(arrayToSearch)
    if arrayToSearch(idx) == sqrt(target)
        return
    end
end

运行后,函数最后结果为:

 

 

 

 

 

 

相关推荐
很实用的时频分析工具箱,包括短时傅里叶正反变换等,很好,很强大哦~~分享给大家,包括以下函数的源代码: % sigmerge - Add two signals with given energy ratio in dB. % % Choice of the Instantaneous Amplitude % amexpo1s - Generate one-sided exponential amplitude modulation. % amexpo2s - Generate bilateral exponential amplitude modulation. % amgauss - Generate gaussian amplitude modulation. % amrect - Generate rectangular amplitude modulation. % amtriang - Generate triangular amplitude modulation. % % Choice of the Instantaneous Frequency % fmconst - Signal with constant frequency modulation. % fmhyp - Signal with hyperbolic frequency modulation. % fmlin - Signal with linear frequency modulation. % fmodany - Signal with arbitrary frequency modulation. % fmpar - Signal with parabolic frequency modulation. % fmpower - Signal with power-law frequency modulation. % fmsin - Signal with sinusoidal frequency modulation. % gdpower - Signal with a power-law group delay. % % Choice of Particular Signals % altes - Altes signal in time domain. % anaask - Amplitude Shift Keyed (ASK) signal. % anabpsk - Binary Phase Shift Keyed (BPSK) signal. % anafsk - Frequency Shift Keyed (FSK) signal. % anapulse - Analytic projection of unit amplitude impulse signal. % anaqpsk - Quaternary Phase Shift Keyed (QPSK) signal. % anasing - Lipschitz singularity. % anastep - Analytic projection of unit step signal. % atoms - Linear combination of elementary Gaussian wave packets. % dopnoise - Generate complex Doppler random signal. % doppler - Generate complex Doppler signal. % klauder - Klauder wavelet in time domain. % mexhat - Mexican hat wavelet in time domain. % tftb_window - Window generation (previously window.m). % % Addition of Noise % noisecg - Analytic complex gaussian noise. % noisecu - Analytic complex uniform noise. % % Modification % scale - Scale signal using Mellin transform. % % % Processing Files % % Time-Domain Process
©️2020 CSDN 皮肤主题: 1024 设计师:白松林 返回首页