Main Content


Store data in the internal memory of a (file or monitor) logger object

Since R2022b



    store(lgr,context,data,iter) stores data into lgr internal memory, grouped in a variable named context and associated with iteration iter.


    collapse all

    This example shows how to log data to disk when training an agent using a custom training loop.

    Create a FileLogger object using rlDataLogger.

    flgr = rlDataLogger();

    Set up the logger object. This operation initializes the object performing setup tasks such as, for example, creating the directory to save the data files.


    Within a custom training loop, you can now store data to the logger object memory and write data to file.

    For this example, store random numbers to the file logger object, grouping them in the variables Context1 and Context2. When you issue a write command, a MAT-file corresponding to an iteration and containing both variables is saved with the name specified in flgr.LoggingOptions.FileNameRule, in the folder specified by flgr.LoggingOptions.LoggingDirectory.

    for iter = 1:10
        % Store three random numbers in memory 
        % as elements of the variable "Context1"
        for ct = 1:3
            store(flgr, "Context1", rand, iter);
        % Store a random number in memory 
        % as the variable "Context2"
        store(flgr, "Context2", rand, iter);
        % Write data to file every 4 iterations
        if mod(iter,4)==0

    Clean up the logger object. This operation performs clean up tasks like for example writing to file any data still in memory.


    Input Arguments

    collapse all

    Data logger object, specified as either a FileLogger or a MonitorLogger object.

    Name of the saved variable, specified as either a string or character vector. All data associated with iteration iter and the name context is vertically concatenated in a single MATLAB® variable named context. This variable is then written to the logger target (either a MAT-file or a trainingProgressMonitor object) when write is invoked for lgr.

    Data to be saved, specified as any fundamental MATLAB datatype. Data associated with the same iteration and the same context name is grouped in a single variable.

    Iteration number, specified as a positive integer. When store is executed multiple times with the same iteration number, data is appended to the memory entry for that iteration. This memory entry, which can contain several context variables, is then written to the logger target as a single unit (for example as a single MAT-file) when write is invoked for lgr.

    Version History

    Introduced in R2022b