Scilab Website | Contribute with GitLab | Mailing list archives | ATOMS toolboxes
Scilab Online Help
2023.0.0 - English


setfield

change the value of an element of a mlist, tlist or list

Syntax

uL = setfield(a, v, L)

Arguments

L
a list, tlist or mlist list: the list whose component must be changed.

a
Address of the component to change. It can be either its field name specified as a string, or its index specified as a positive integer. See insertion for more details.

v
Any Scilab object: the value to assign.

uL
The updated list.

Description

L=setfield(a,v,L) addresses and changes the ith element of a list, in a unified way over lists, tlists and mlists.

For simple lists and for tlists, L=setfield(a,v,L) is equivalent to L(a)=v, that is simpler and should be preferred. The same stands for any mlist if the address a is a field's name.

However, for any mlist L, if i is an index, the insertion L(i)=v is not predefined, in order to let the user define an overload implementing a matrix-oriented insertion, if required. Yet setfield(i,v,L) is always defined, with the same meaning as for other list and tlist types.

Examples

For a tlist, setfield(3, v, T) is equivalent to T(3)=v :

T = tlist(['V','a','b'], [%z (1-%z) %z^2], [%f %t %t]);
// The insertion is predefined:
T(3) = [%t %t %f];
T(3)

T = setfield(3, [%f %t %f], T);
T(3)
--> T = tlist(['V','a','b'], [%z (1-%z) %z^2], [%f %t %t]);
--> // The insertion is predefined:
--> T(3) = [%t %t %f];
--> T(3)
 ans  =
  T T F

--> T = setfield(3, [%f %t %f], T);
--> T(3)
 ans  =
  F T F

For a mlist, there is no predefined indexed insertion routine, to let the user define a matrix-oriented insertion overload if required.

M = mlist(['V','a','b'], [%z (1-%z) %z^2], [%f %t %t]);
M.b = [%t %t %t];
M.b                 // OK, while ...
M(3) = [%f %t %f];  // => error

// But a raw direct insertion with setfield() is always possible:
M = setfield(3, [%t %f %t], M);
getfield(3, M)
--> M = mlist(['V','a','b'], [%z (1-%z) %z^2], [%f %t %t]);
--> M.b = [%t %t %t];
--> M.b                 // OK, while ...
 ans  =
  T T T

--> M(3) = [%f %t %f];  // => error
Function not defined for given argument type(s),
  check arguments or define function %b_i_V for overloading.

--> // But a raw direct insertion with setfield() is always possible:
--> M = setfield(3, [%t %f %t], M);
--> getfield(3, M)
 ans  =
  T F T

See also

  • insertion — partial variable assignation or modification
  • getfield — list field extraction

History

VersionDescription
6.0.0
  • The updated list is now returned, instead of being changed "in place".
  • setfield() can no longer be used for cells arrays.
Report an issue
<< rlist Data Structures struct >>

Copyright (c) 2022-2024 (Dassault Systèmes)
Copyright (c) 2017-2022 (ESI Group)
Copyright (c) 2011-2017 (Scilab Enterprises)
Copyright (c) 1989-2012 (INRIA)
Copyright (c) 1989-2007 (ENPC)
with contributors
Last updated:
Mon Mar 27 11:52:45 GMT 2023