Browse Course Material
Course info.
- Orhan Celiker
Departments
- Electrical Engineering and Computer Science
As Taught In
- Graphics and Visualization
- Programming Languages
Learning Resource Types
Introduction to matlab, course description.
You are leaving MIT OpenCourseWare
Assign a value to a workspace variable
- assignin( ws ,' var ', val )
assignin ( ws ,' var ', val ) assigns the value val to the variable var in the workspace ws . var is created if it doesn't exist. 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)
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 M-file. Note the base and caller workspaces are equivalent in the context of an M-file that is invoked from the MATLAB command line.
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});
Help Center Help Center
- Help Center
- Trial Software
- Product Updates
- Documentation
Assign value to structure array field
Description
S = setfield( S , field , value ) assigns a value to the specified field of the structure S . For example, S = setfield(S,'a',1) makes the assignment S.a = 1 .
As an alternative to setfield , use dot notation: S.field = value . Dot notation is typically more efficient.
If S does not have the specified field, then setfield creates it and assigns value to it.
S = setfield( S , field 1,..., field N, value ) assigns a value to the specified field of a nested structure. For example, S = setfield(S,'a','b','c',1) makes the assignment S.a.b.c = 1 , where the fields S.a and S.a.b are also structures.
S = setfield( S , idx , field 1,..., field N, value ) specifies an element of S and assigns a value to one of its fields. For example, S = setfield(S,{3,4},'a',1) makes the assignment S(3,4).a = 1 .
S = setfield( S , idx , field 1, idx 1,..., field N, idx N, value ) specifies elements of fields. For example, S = setfield(S,'a',{2},1) makes the assignment S.a(2) = 1 . Similarly, S = setfield(S,{3,4},'a',{2},'b',1) makes the assignment S(3,4).a(2).b = 1 .
collapse all
Assign Values to Fields
Create a scalar structure.
Assign a value to a field using the setfield function.
Assign a value to another field. If you specify a field that does not exist, then setfield creates it.
You also can assign a value to a field using dot notation.
Field of Nested Structure
Create a nested structure. In a nested structure, a structure at any level can have fields that are structures, and other fields that are not structures.
While S is a structure, the fields S.a , S.a.b , and S.a.b.e are also structures.
Assign a value to S.a.b.d using the setfield function. When you specify a comma-separated list of nested structure names, include the structure names at every level between the top and the field name you specify. In this case, the comma-separated list of structure names is 'a','b' and the field name is 'd' .
You also can use dot notation to assign a value.
Fields of Elements of Structure Array
Assign values to fields of elements of a structure array.
First, create a structure array. As in all structure arrays, each element is a structure with the same fields.
You also can assign values using setfield . If a field does not exist, setfield creates it. Create a field named title .
The setfield function assigns a value to a field of an individual element, but the output argument is the entire structure array.
Display the first element of S .
As an alternative, index into the structure array, and then use dot notation to assign a value to a field of an element.
Indices of Nested Structure Array
Assign a value to a field of a nested structure, in which the structures at some levels are structure arrays. In this example, S is a 1-by-2 structure array. The second element, S(2) , has a nested structure a.b , where b is a 1-by-3 structure array.
First, create a nested structure. After creating the structure using dot notation, create another nonscalar structure array using the struct function and add it as a field.
Display the third element of S(2).a.b .
Assign a new value to the field d of S(2).a.b(3) using the setfield function. Display the structure with the updated field.
Elements of Field
Create a structure with a field whose value is an array.
Assign values to elements of S.a using the setfield function. To assign values to particular elements, specify indices after the name of the field. You must specify the indices within a cell array. However, specify the new values in an array whose data type matches the data type of the field.
You also can use dot notation and array indexing to assign values to the same elements.
Input Arguments
S — structure array structure array.
Structure array. If S is nonscalar, then each element of S is a structure, and all elements have the same fields with the same names.
field — Field name character vector | string scalar
Field name, specified as a character vector or string scalar.
idx — Indices cell array of numeric or logical values
Indices, specified as a cell array of numeric or logical values. Indices for S and fields 1 through N-1 specify individual elements of structure arrays. Indices for field N specify one or more elements of the array in that field, which can be of any type.
Example: S = setfield(S,{1,2},'a',1) is equivalent to S(1,2).a = 1 .
Example: If S.a = [5 10 20] , then S = setfield(S,'a',{[2,3]},[50 100]) is equivalent to S.a(2:3) = [50 100] .
value — Values array
Values, specified as any type of array having any size.
Extended Capabilities
C/c++ code generation generate c and c++ code using matlab® coder™..
Usage notes and limitations:
Field name must be constant.
Thread-Based Environment Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool .
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment .
Version History
Introduced before R2006a
getfield | fieldnames | isfield | orderfields | rmfield | struct
- Structure Arrays
- Generate Field Names from Variables
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
- Switzerland (English)
- Switzerland (Deutsch)
- Switzerland (Français)
- 中国 (English)
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
- América Latina (Español)
- Canada (English)
- United States (English)
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
Contact your local office
IMAGES
COMMENTS
Syntax. assignin(ws,var,val) Description. assignin(ws,var,val) assigns the value val to the variable var in the workspace ws. For example, assignin('base','x',42) assigns the value 42 to the variable x in the MATLAB ® base workspace.
You can create new variables in the workspace by running MATLAB code or using existing variables. To create a new variable, enter the variable name in the Command Window, followed by an equal sign ( = ) and the value you want to assign to the variable.
You can assign any or all consecutive elements of a comma-separated list to variables with a simple assignment statement. Define the cell array C and assign the first row to variables c1 through c6 .
This section contains a compilation of all the exercises (21 in total) presented in the course.
The availability of technical computing environment such as MATLAB is now reshaping the role and applications of computer laboratory projects to involve students in more intense problem-solving experience.
The syntax c{:} transforms a cell array in a list, and a list is a comma separated values, like in function arguments. Meaning that you can use the c{:} syntax as argument to other functions than deal. To see that, try the following: > z = plus(1,2) z = 3. > c = {1,2};
This homework is designed to give you practice with more advanced and specific Matlab functionality, like advanced data structures, images, and animation. As before, the names of helpful functions are provided in bold where needed.
This is an accelerated introduction to MATLAB® and its popular toolboxes. Lectures are interactive, with students conducting sample MATLAB problems in real time. The course includes problem-based MATLAB assignments. Students must provide their own laptop and software.
The assignin function is used to export the user-entered values to the MATLAB workspace variables imfile and cmap. 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}); See Also.
Assign values to elements of S.a using the setfield function. To assign values to particular elements, specify indices after the name of the field. You must specify the indices within a cell array. However, specify the new values in an array whose data type matches the data type of the field.