MATLAB: Warm Start Design

I am currently encoding a function in MATLAB, which has two steps:

  • It processes some data.
  • It performs some calculations of the processed data.

I use the function to start a series of experiments, where I set up the calculation method in step 2).

After seeing how I work with a large data set, the data processing step takes some time each time, however, I wonder if there is any code for a β€œwarm start” in the function.

That is, one way or another, to save all the progress that I made after step 1), so if I run the function a second or third time, it can skip step 1) and go to step 2)?

EDIT: Thank you all for your suggestions. There are several ways to do this - and I think the Kitsune proposal is best for my situation.

+3
source share
4 answers
function data = get_data(varargin)
persistant stored_data
if nargin>1 && any(strcmp(varargin,'--reload'))
  stored_data=[];
end
if isempty(stored_data)
  stored_data = ...;
end

return stored_data;
+2
source

One way to do this is to write a class instead of a function. Below is a very simple class that you can extend as much as you want. This requires that you have divided two steps in its function into two functions are called runFirstStep, and runSecondStepin my example.

You run it like

obj = myAwesomeClass;
finalResults = obj.run(inputData); %# pass inputData for the first step if necessary

The intermediate result is stored in obj.intermediateResults, so if you call again run, the first step is automatically omitted.

, get intermediateResults. set , , get ( ) , , .

classdef myAwesomeClass<handle
properties
   intermediateResults
end 
methods
   function finalResults = run(obj,inputData)
        %# run accepts the object and runs both steps, if necessary
        %# inputData is the data that is processed (if it not loaded inside runFirstStep)

        %# the first step only needs to run if there are no intermediate results
        if isempty(obj.intermediateResults)
           obj.intermediateResults = runFirstStep(inputData);
        end

        %# we always want to run the second step
        finalResults = runSecondStep(obj.intermediateResults);
    end
end
end

, finalResults , set/get , plot, . , , .

+1

, , , 2 ( ).

, 1 ( ). , . 2.

+1

For this kind of problem, I would like to distinguish two stages into two separate functions. One generates data, saving it as structure fields. Then I save this structure. The script wizard executes both parts if necessary.

function mystruct = gen_data( ... )
    mystruct.field = ...;
end

Used once to do this:

mystruct = gen_data( ... );
save(mystruct, 'mystruct.mat');

The main processing function takes this structure as one of its arguments:

function result = process_data(mystruct, ...)
    % do stuff
end

I usually start processing through a script wizard. I also save my workspaces and saved variables in version control in parallel with my code and history, so I can work in reverse order to do what was done as a result.

if (~exist('mystruct'))
    load('mystruct.mat');
end
result = process_data(mystruct);
+1
source

Source: https://habr.com/ru/post/1784875/


All Articles