What is an effective way to save the result in Matlab if I need to stop the iteration for some reason?

I am trying to minimize my objective function by doing iterations, and this can take a lot of time (maybe a day or more). If for some reason I have to stop the program or it freezes, then what is an effective way to save the results of iterations that could be executed until the program stops for some reason.

The variables I want to save are: best_obj_fun andk_to_perturb_annealing_best

NOTE: I save best_obj_funmine in the matrix, and I only need the value during the last iteration fork_to_perturb_annealing_best

See the last loop in the following code for the variables that I want to have when the program is somehow stopped (for example, CTRL + C):

%% Running iterations to find minimum objective function value

accept_iter=1;
for iter=1:100
    %# Choosing 'nRandomPoints' random points on the grid to sample from 'fixed_underlying_distribution'
    nRandomPoints=100;
    random_grid_points=randi(sqrt(nCell),[nRandomPoints,2]); %# two columns with x and y coord resp.
    repeated_set_of_coord=ismember(random_grid_points,fixed_grid_coordinates,'rows'); %# find repeated sets of coordinates

    while any(repeated_set_of_coord)
        random_grid_points(repeated_set_of_coord,:)=randi(sqrt(nCell),[sum(repeated_set_of_coord),2]); %# create new set of coordinates
        repeated_set_of_coord(repeated_set_of_coord)=ismember(random_grid_points(...
            repeated_set_of_coord,:),fixed_grid_coordinates,'rows'); %# check the new coordinates
    end
    linear_index_for_perturbation=sub2ind([sqrt(nCell),sqrt(nCell)],random_grid_points(:,1),...
        random_grid_points(:,2)); %# get a linear index into matrix to be perturbed
    k_to_perturb_annealing_initial=k_sisim_for_annealing;

    k_to_perturb_annealing_initial(linear_index_for_perturbation)=emprand(k_20_sampledata_sisim,nRandomPoints,1);

    %# computing the value of objective function for perturbed perm values
    [new_obj_fun,new_k_geomean]=obj_fun_for_gibbs_sampling(k_to_perturb_annealing_initial,x_200x200_gslib_format_vector...
        ,y_200x200_gslib_format_vector,gamma_k_underlying,mean_gamma_k_underlying,k_eff_WT);

    if  new_obj_fun < best_obj_fun(accept_iter)
        best_obj_fun(accept_iter+1)=new_obj_fun;
        k_to_perturb_annealing_best=k_to_perturb_annealing_initial;
        best_k_geomean=new_k_geomean;
        accept_iter=accept_iter+1;
    end
end
+3
2

matlab, - assignin for. , , .

+1

save -append .

, , . .

A=zeros(...);%# this is an example of your variables that are initialized before the loop
save('filename','A');%# this creates a .mat file with the initialized variables

%#begin loop
for i=...
    ...
    ... %# some computations here

    save('filename','A','-append') %# this just replaces the variables in the .mat file with their current values
end
%# end loop

, ..

save('filename','A(i)','-append')

n - , . , , n .

+3

All Articles