MATLAB Data Files

MATLAB data files let you keep measurements and computed results independently of an open session. A MAT-file is a binary file that stores named MATLAB variables, including their values, sizes and data types. You use these files to resume an analysis or pass results to another calculation without repeating the work that produced them.

Separate stored data from live variables

The workspace holds the variables you are using now. A MAT-file holds copies written at a particular time. The governing idea is:


workspace values→savestored copies→loadrestored values\text{workspace values} \xrightarrow{\text{save}} \text{stored copies} \xrightarrow{\text{load}} \text{restored values}


Changing a variable after saving does not update its stored copy. Likewise, loading data does not update the file. Saving an ordinary analysis script preserves its commands; use a data file to preserve the values those commands produced.


A workspace connects through a MAT-file to data loaded into a new structure.


Choose what to save or update

To save selected measurements and their associated times, write:


MATLAB
save("runA.mat","time_s","force_N")


The quoted names identify existing workspace variables. MATLAB writes the file in the current folder when you provide only a filename. For another location, supply a relative or full file path. Checking the location matters when several project folders contain similarly named files.


Choose the operation according to what you intend to keep:


Command

Effect on the MAT-file

save("runA.mat")

Saves every current workspace variable and replaces an existing file.

save("runA.mat","force_N")

Saves only force_N and replaces an existing file.

save("runA.mat","force_N","-append")

Updates force_N while retaining other variables already stored.


Selecting variables makes the saved dataset easier to understand: a temporary calculation need not travel with your measurements. Include supporting information such as units, sampling times or calibration factors when another calculation needs it. A name such as force_N helps communicate units, but MATLAB does not infer physical units from the name.



Inspect and restore deliberately

Before loading, inspect the file:


MATLAB
whos("-file","runA.mat")


This lists saved variable names, sizes and types without adding those variables to your workspace. Plain whos instead describes the current workspace. Inspection helps you confirm that the intended dataset is present before your calculation depends on it.


Direct loading restores variables under their saved names:


MATLAB
load("runA.mat","force_N")


Here, an existing workspace force_N is replaced. Other workspace variables remain as they were. For comparing saved and current measurements, load into a structure, a container with named fields:


MATLAB
S = load("runA.mat","force_N");
savedForce_N = S.force_N;


The field S.force_N contains the saved array; the separate workspace force_N is unchanged. Choose an output name that is not already holding data you need. Assigning the result to S replaces any previous S.



What to remember

  • Saving copies values at that moment; later edits require another save.
  • Ordinary saving replaces a file; -append retains other saved variables.
  • Inspect file contents with whos("-file",filename).
  • Direct loading replaces matching names; structure output keeps imported fields together.

Example: Preserve and reuse a force measurement

Example: A laboratory records three force measurements and their sampling times. A data snapshot is a saved copy of selected values at a particular moment. The laboratory wants a snapshot that includes the measurements and their dimensionless calibration factor, but excludes temporary work.


The workspace initially contains:


MATLAB
time_s = [0 2 4];
force_N = [8 12 16];
gain = 1.25;
scratch = 999;


The times are in seconds and the forces are in newtons. Assume the destination file does not yet exist, all file operations succeed, and the output names S and check are unused.


a) Save only time_s, force_N and gain in force_trial.mat. What is the mean force in this saved dataset?


First, name the destination and each variable to retain. Listing the variables explicitly excludes scratch.


MATLAB
save("force_trial.mat","time_s","force_N","gain")


There are ________ force readings, and their sum is ________. Divide that sum by the number of readings to obtain the mean:


F‾=8+12+163=12 N\overline{F}=\frac{8+12+16}{3}=12\ \text{N}


The file contains time_s, force_N and gain. Its saved force readings have a mean of 12 N.


b) Later, the workspace variables change as shown below. Restore the saved forces and gain while preserving these current workspace values, then calculate the calibrated mean force from the saved data.


MATLAB
force_N = [30 30 30];
gain = 2;


Load the selected saved variables into the unused output name S. This creates fields containing the imported values and leaves the separate workspace force_N and gain unchanged.


MATLAB
S = load("force_trial.mat","force_N","gain");
calibratedMean_N = S.gain * mean(S.force_N);


The saved calibration factor is ________, and the saved mean force is ________. The current workspace values do not enter this expression because both inputs are accessed through S.


F‾calibrated=(1.25)(12 N)=15 N\overline{F}_{\text{calibrated}}=(1.25)(12\ \text{N})=15\ \text{N}


The calibrated mean force is 15 N. The separate workspace variables remain force_N = [30 30 30] and gain = 2.


c) Calculate every calibrated force from the saved data. Add the resulting variable corrected_N to the same file while retaining its existing contents. State the calibrated array and the variables now stored in the file.


Multiply the saved force array by the saved dimensionless gain. This applies the same calibration factor to every reading.


Fcorrected=1.25[81216]=[101520] N\mathbf{F}_{\text{corrected}}=1.25[8\quad12\quad16]=[10\quad15\quad20]\ \text{N}


Create the derived variable, then use append to add it without replacing the other saved variables.


MATLAB
corrected_N = S.gain * S.force_N;
save("force_trial.mat","corrected_N","-append")
whos("-file","force_trial.mat")


The inspection lists four variables: time_s, force_N, gain and corrected_N. To check the numeric result explicitly, restore just the derived array:


MATLAB
check = load("force_trial.mat","corrected_N");
check.corrected_N


The calibrated array is [10 15 20] N, and the file retains the three original saved variables alongside corrected_N. The later workspace values of force_N and gain have not been saved over the original measurements.

A student has sensor readings in voltage_V and sampling times in time_s. The student wants to close MATLAB and later restore both current arrays without repeating the experiment. Which command explicitly stores both arrays in sensor.mat?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Your workspace, the collection of variables currently held in memory, contains measurements from an unfinished experiment. You want to inspect trial_A.mat, a MAT-file in the current folder, before deciding what to import.


Complete the command below to list the saved variable names, dimensions and classes without loading their values into the workspace.


-file trial_A.mat

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

An existing file named bending.mat contains a variable called force_N. Your workspace already contains different readings called force_N that you need to keep. The output name trial is unused. Complete the command name and field expression for importing the saved readings while preserving the existing workspace array.


Assign the output of to trial when reading bending.mat. Then access the imported force readings using .

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Extra Practice