matlab - 在 switch-case MATLAB 中执行多个案例

标签 matlab

我想根据用户输入转到脚本的特定部分,然后从那里继续并执行整个脚本。我尝试使用 switch case 来实现它,但问题是在 matlab 中的 switch case 中,当我们没有显式调用break时,不会陷入下一个情况。但我的程序逻辑是,我需要转到一个案例,并执行之后的所有案例,包括该案例。如何在 MATLAB 中实现这一目标?

这是我的脚本供引用:-

clear all;
close all;
clc;

disp('This is a script to do the back projection for an event given the required input files.');
disp('Please make sure you have copied all of the following files into the ./back_projection/data directory:-');
disp(' ');
disp('1. V_{array}.txt - File containing the velocity data of all the stations selected from the array in one single column');
disp('2. T_{array}.txt - FIle containing the corresponding time data for rall the stations selected from the array in one single column');
disp('3. P_time_{array} - File containing the P wave arrival time of the event at all all the selected stations of the array');
disp('4. stnlat_{array} - File containing the station latitudes of all the stations selected from the array');
disp('5. stnlong_{array} - File containing the station longitudes of all the stations selected from the array');
disp('6. data_info_{array}.txt - File containing the GCARC, AZ and BAZ of the event at all the stations selected from the array');
disp(' ');
input('Continue? (After making sure, press enter)','s');
clc

disp('Choose from where to start processing :-');
disp('0. Generate grid (Start from the beginning)');
disp('1. Station check');
disp('2. Cross correlation');
disp('3. Rupture back projection');
disp('4. Net stack alignment');
disp('5. Cumulative plot of energy');
disp('6. Plot of energy peaks at each second');
disp('7. Saving movie frames');
disp('8. Run a movie test');
disp('9. Stack plots for STF calculation');
disp('10. STF plot & calculation');
disp('11. Stack plots for plotting earthquake traces');
disp('12. Plot of all earthquake traces');
disp(' ');
listener=input('Enter your choice: ');
clc

switch listener
%% Starting back projection
    case 0
    delete array.txt;
    cd back_projection;
    gridgen;
    save variables/vars0;
    opts=input('Do you want to quit the program? (y/n)','s');
    if(opts=='y')
        return
    end

    case 1
    case 2
    case 3
    path=strsplit(pwd,'\');
    pathcheck=strcmp(cell2mat(path(end)),'back_projection');
    if (pathcheck==0)
    cd back_projection;
    end
    clear;
    load variables/vars0;
    o='y';

    a_arr = [];
    increment = 0;

    while(strcmpi(o,'y'))
        array = input('Enter array: ','s');
        disp(' ');
        disp('Station Check...');
        station_check;
        save variables/vars1;
        opts=input('Do you want to quit the program? (y/n)','s');
        if(opts=='y')
            return
        end
        clear;
        load variables/vars1;
        increment = increment+1;
        a_arr = [a_arr;array];
        disp(' ');
        disp('Cross Correlation...');
        Crosscorrelation;
        save variables/vars2;
        opts=input('Do you want to quit the program? (y/n)','s');
        if(opts=='y')
            return
        end
        clear;
        load variables/vars2;
        disp(' ');
        disp('Back Projection...')
        rupture_back_projection;
        save variables/vars3;
        opts=input('Do you want to quit the program? (y/n)','s');
        if(opts=='y')
            return
        end
        clear;
        load variables/vars3;
        o = input('Do you want to enter more arrays (y/n)? ','s');

    end

    cd ..;
    fid = fopen('array.txt','w');
    for j = 1:increment
        fprintf(fid,'%s \n',a_arr(j,:));
    end
    fclose(fid);
    cd back_projection;
    %%
    case 4
    load variables/vars3;
    path=strsplit(pwd,'\');
    pathcheck=strcmp(cell2mat(path(end)),'back_projection');
    if (pathcheck==0)
    cd back_projection;
    end
    disp(' ');
    disp('Aligning all the arrays...');
    net_stack_align;
    save variables/vars4;
    opts=input('Do you want to quit the program? (y/n)','s');
    if(opts=='y')
        return
    end
    case 5
    path=strsplit(pwd,'\');
    pathcheck=strcmp(cell2mat(path(end)),'back_projection');
    if (pathcheck==0)
    cd back_projection;
    end
    clear;
    load variables/vars4;
    %%
    input('Close the plot and press enter');
    disp(' ');
    disp('Doing a cumulative plot of the energy in the provided time window...');
    start_win = input('Enter the starting time of the window: ');
    end_win = input('Enter the closing the of the window: ');
    cumulative_plot(b_corr,e_lat,e_long,start_win,end_win,ev_lat,ev_long);
    input('Close the plot and press enter');
    save variables/vars5;
    opts=input('Do you want to quit the program? (y/n)','s');
    if(opts=='y')
        return
    end
    case 6
    path=strsplit(pwd,'\');
    pathcheck=strcmp(cell2mat(path(end)),'back_projection');
    if (pathcheck==0)
    cd back_projection;
    end
    clear;
    load variables/vars5;
    %%
    disp(' ');
    disp('Plotting the energy peak at all points in time (secs) within the time window...');
    end_win = input('Enter the closing time of the window (window starts from 1 second): ');
    peak_time_wise(b_corr,e_lat,e_long,end_win,ev_lat,ev_long);
    input('Close the plot and press enter');
    save variables/vars6;
    opts=input('Do you want to quit the program? (y/n)','s');
    if(opts=='y')
        return
    end
    case 7
    path=strsplit(pwd,'\');
    pathcheck=strcmp(cell2mat(path(end)),'back_projection');
    if (pathcheck==0)
    cd back_projection;
    end
    clear;
    load variables/vars6;

    disp(' ');
    disp('Saving movie frames ...');
    movie_slices(b_corr, e_lat, e_long, bp_l, bp_u);
    disp('Saved');
    save variables/vars7;
    opts=input('Do you want to quit the program? (y/n)','s');
    if(opts=='y')
        return
    end

    case 8
    path=strsplit(pwd,'\');
    pathcheck=strcmp(cell2mat(path(end)),'back_projection');
    if (pathcheck==0)
    cd back_projection;
    end
    clear;
    load variables/vars7;
    %%
    disp(' ');
    disp('Running a movie test');
    frameskip = input('Enter the number of frames to skip for testing: ');
    movie_test(b_corr,e_lat,e_long,bp_l,bp_u,frameskip);
    input('Close the plot and press enter');
    save variables/vars8;
    opts=input('Do you want to quit the program? (y/n)','s');
    if(opts=='y')
        return
    end

    case 9
    path=strsplit(pwd,'\');
    pathcheck=strcmp(cell2mat(path(end)),'back_projection');
    if (pathcheck==0)
    cd back_projection;
    end
    clear;
    load variables/vars8;

    %% Miscellaneous Calculations
    disp(' ');
    disp('Back Projection has been done. Starting miscellaneous calculations.');
    disp('STF calculation...');
    o='y';

    while(strcmpi(o,'y'))
        array=input('Enter the array: ','s');
        stack_plot;
        movefile(fullfile('output',['stack_uncorr_',num2str(bp_l),'_',num2str(bp_u),'Hz_',array,'.txt']),fullfile('output',['stack_uncorr_',num2str(bp_l),'_',num2str(bp_u),'Hz_',array,'_stf.txt']));
        movefile(fullfile('output',['stack_corr_',num2str(bp_l),'_',num2str(bp_u),'Hz_',array,'.txt']),fullfile('output',['stack_corr_',num2str(bp_l),'_',num2str(bp_u),'Hz_',array,'_stf.txt']));
        save variables/vars9;
        opts=input('Do you want to quit the program? (y/n)','s');
        if(opts=='y')
            return
        end
        clear;
        load variables/vars9;
        o=input('Do you want to enter more arrays? (y/n) ','s');
    end
    case 10
    loadd variables/vars9;
    path=strsplit(pwd,'\');
    pathcheck=strcmp(cell2mat(path(end)),'back_projection');
    if (pathcheck==0)
    cd back_projection;
    end
    STF_calc;
    input('Close the plot and press enter');
    save variables/vars10;
    opts=input('Do you want to quit the program? (y/n)','s');
    if(opts=='y')
        return
    end
    case 11
    case 12
    path=strsplit(pwd,'\');
    pathcheck=strcmp(cell2mat(path(end)),'back_projection');
    if (pathcheck==0)
    cd back_projection;
    end
    clear;
    load variables/vars10;
    %%
    disp(' ');
    disp('Plotting earthquake traces...');
    o='y';
    while(strcmpi(o,'y'))
        array=input('Enter the array: ','s');
        stack_plot;
        trace_plot;
            save variables/vars11;
        opts=input('Do you want to quit the program? (y/n)','s');
        if(opts=='y')
            return
        end
        clear;
        load variables/vars11;
        o=input('Do you want to enter more arrays? (y/n) ','s');
    end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% To add any additional scripts, put the scripts in the './back_projection'
% directory  and then insert the script call in this section below.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
disp(' ');
disp('Process completed');
cd ..;

最佳答案

我的首选方法是将代码分成函数(可能是 local functions 甚至 private functions ,这具有额外的优点,这使得代码更具可读性,然后让每个函数在其末尾,它在返回之前调用序列中的下一个函数。

这将准确地创建您想要的级联,并且在主脚本中,您只需要一个 switch 语句来为“级联”中的特定步骤调用适当的函数。

例如:

function main_function(Input)
  switch(Input)  % assumes appropriate Input checks and sanitization has occured
    case 1; step1_generate_grid();
    case 2; step2_station_check();
    case 3; step3_cross_correlation();
    case 4; step4_etc_etc();
    otherwise; disp('Nothing to perform');
  end
end

function step1_generate_grid()
  disp('Generating Grid...');
  step2_station_check(); % Go to next step in cascade
end

function step2_station_check()
  disp('Checking station...');
  step3_cross_correlation(); % Go to next step in cascade
end

function step3_cross_correlation()
  disp('Performing cross-correlation...');
  step4_etc_etc(); % Go to next step in cascade 
end

function step4_etc_etc()
  disp('Performing final steps etc...');
  % Final step in cascade, no further functions to call.
end

关于matlab - 在 switch-case MATLAB 中执行多个案例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43996741/

相关文章:

matlab - 如何在 MATLAB 中创建 FFT 图?

MATLAB `unstack` : Safe to assume ordering of new columns?

matlab - 在不硬编码输入和输出数量的情况下创建 MIMO(多输入多输出)传递函数系统

matlab - 如何使用matlab做圆形裁剪?

function - matlab中的求和函数句柄

c++ - 更改 mex 函数中的常量参数

matlab:如何打印字符串和变量内联

matlab - 如何在Matlab中的文件夹中查找.xlsx文件?

image - 在Matlab中优化一个简单的函数(直方图距离)

matlab - 如何检查 MATLAB 句柄是否有效?