如何根据MATLAB中的条件从中间开始运行脚本?

时间:2017-05-15 15:56:42

标签: matlab

我有一个脚本需要花费大量时间才能运行,并且生成的一些结果可能不太令人满意,我们可能需要重新运行部分脚本。

所以我已经选择了在需要的地方退出程序并将所有工作区变量保存到文件中。但是当用户稍后再次运行脚本时,我不希望脚本从头开始运行。我想询问用户他们已经取得了多大进展,并且从那里继续。

一个可能的解决方案是使用goto,这在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');

%% Starting back projection
delete array.txt;
cd back_projection;
gridgen;
save vars;
opts=input('Do you want to quit the program? (y/n)','s');
if(opts=='y')
    return
end
clear;
load vars;
o='y';
a_arr = [];
increment = 0;
while(strcmpi(o,'y'))
    array = input('Enter array: ','s');
    disp(' ');
    disp('Station Check...');
    station_check;
    save vars;
    opts=input('Do you want to quit the program? (y/n)','s');
    if(opts=='y')
        return
    end
    clear;
    load vars;
    increment = increment+1;
    a_arr = [a_arr;array];
    disp(' ');
    disp('Cross Correlation...');
    Crosscorrelation;
    save vars;
    opts=input('Do you want to quit the program? (y/n)','s');
    if(opts=='y')
        return
    end
    clear;
    load vars;
    disp(' ');
    disp('Back Projection...')
    rupture_back_projection;
    save vars;
    opts=input('Do you want to quit the program? (y/n)','s');
    if(opts=='y')
        return
    end
    clear;
    load vars;
    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;
%%
disp(' ');
disp('Aligning all the arrays...');
net_stack_align;
save vars;
opts=input('Do you want to quit the program? (y/n)','s');
if(opts=='y')
    return
end
clear;
load vars;
%%
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 vars;
opts=input('Do you want to quit the program? (y/n)','s');
if(opts=='y')
    return
end
clear;
load vars;
%%
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 vars;
opts=input('Do you want to quit the program? (y/n)','s');
if(opts=='y')
    return
end
clear;
load vars;

disp(' ');
disp('Saving movie frames ...');
movie_slices(b_corr, e_lat, e_long, bp_l, bp_u);
disp('Saved');
save vars;
opts=input('Do you want to quit the program? (y/n)','s');
if(opts=='y')
    return
end
clear;
load vars;
%%
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 vars;
opts=input('Do you want to quit the program? (y/n)','s');
if(opts=='y')
    return
end
clear;
load vars;

%% 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 vars;
    opts=input('Do you want to quit the program? (y/n)','s');
    if(opts=='y')
        return
    end
    clear;
    load vars;
    o=input('Do you want to enter more arrays? (y/n) ','s');
end
STF_calc;
input('Close the plot and press enter');
save vars;
opts=input('Do you want to quit the program? (y/n)','s');
if(opts=='y')
    return
end
clear;
load vars;
%%
disp(' ');
disp('Plotting earthquake traces...');
o='y';
while(strcmpi(o,'y'))
    array=input('Enter the array: ','s');
    stack_plot;
    trace_plot;
        save vars;
    opts=input('Do you want to quit the program? (y/n)','s');
    if(opts=='y')
        return
    end
    clear;
    load vars;
    o=input('Do you want to enter more arrays? (y/n) ','s');
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 ..;

1 个答案:

答案 0 :(得分:2)

按照

的方式做点什么
c = input('Which script?');
switch c
case -1
    % first script
case 0
    % second script
case 1
   % third Script
otherwise
   % fourth script
end

如果你不这样做,你也可以在Matlab中阻止注释一些代码。

%{
    Stuff to be commented out
%}