Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Loading Variables within a Function

If you define a function that loads data from a MAT-file, and find that MATLAB® does not return the expected results, check whether any variables in the MAT-file share the same name as a MATLAB function. Common variable names that conflict with function names include i, j, mode, char, size, and path.

For example, consider a MAT-file with variables height, width, and length. If you load these variables using a function such as findVolume,

function vol = findVolume(myfile)
  load(myfile);
  vol = height * width * length;

MATLAB interprets the reference to length as a call to the MATLAB length function, and returns an error:

Error using length
Not enough input arguments.

To avoid confusion, when defining your function, choose one (or more) of the following approaches:

  • Load into a structure array. For example, define the findVolume function as follows:

    function vol = findVolume(myfile)
      dims = load(myfile);
      vol = dims.height * dims.width * dims.length;
  • Explicitly include the names of variables in the call to the load function.

  • Initialize variables (e.g., assign to an empty matrix or an empty character vector) within the function before calling load.

To determine whether a particular name is associated with a MATLAB function, use the exist function.

Was this topic helpful?