Documentation Center

  • Trial Software
  • Product Updates
라이선스가 부여된 사용자만 번역 문서를 볼 수 있습니다. 번역 문서를 보려면 로그인하십시오.

assignin

Assign value to variable in specified workspace

Syntax

assignin(ws, 'var', val)

Description

assignin(ws, 'var', val) assigns the value val to the variable var in the workspace ws. The var input must be the array name only; it cannot contain array indices. If var does not exist in the specified workspace, assignin creates it. ws can have a value of 'base' or 'caller' to denote the MATLAB® base workspace or the workspace of the caller function.

The assignin function is particularly useful for these tasks:

  • Exporting data from a function to the MATLAB workspace

  • Within a function, changing the value of a variable that is defined in the workspace of the caller function (such as a variable in the function argument list)

Examples

Example 1

This example creates a dialog box for the image display function, prompting a user for an image name and a colormap name. The assignin function is used to export the user-entered values to the MATLAB workspace variables imfile and cmap.

prompt = {'Enter image name:', 'Enter colormap name:'};
title = 'Image display - assignin example';
lines = 1;
def = {'my_image', 'hsv'};
answer = inputdlg(prompt, title, lines, def);
assignin('base', 'imfile', answer{1});
assignin('base', 'cmap', answer{2});

Example 2

assignin does not assign to specific elements of an array. The following statement generates an error:

X = 1:8;
assignin('base', 'X(3:5)', -1);

However, you can use the evalin function to do this:

evalin('base','X(3:5) = -1')
X =
     1     2    -1    -1    -1     6     7     8

More About

expand all

Tips

The MATLAB base workspace is the workspace that is seen from the MATLAB command line (when not in the debugger). The caller workspace is the workspace of the function that called the currently running function. Note that the base and caller workspaces are equivalent in the context of a function that is invoked from the MATLAB command line. For more information, see Base and Function Workspaces.

See Also

Was this topic helpful?