1@c DO NOT EDIT! Generated automatically by munge-texi.pl. 2 3@c Copyright (C) 1996-2019 John W. Eaton 4@c 5@c This file is part of Octave. 6@c 7@c Octave is free software: you can redistribute it and/or modify it 8@c under the terms of the GNU General Public License as published by 9@c the Free Software Foundation, either version 3 of the License, or 10@c (at your option) any later version. 11@c 12@c Octave is distributed in the hope that it will be useful, but 13@c WITHOUT ANY WARRANTY; without even the implied warranty of 14@c MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15@c GNU General Public License for more details. 16@c 17@c You should have received a copy of the GNU General Public License 18@c along with Octave; see the file COPYING. If not, see 19@c <https://www.gnu.org/licenses/>. 20 21@node Data Containers 22@chapter Data Containers 23@cindex containers 24 25Octave includes support for three different mechanisms to contain arbitrary 26data types in the same variable: Structures, which are C-like, and are indexed 27with named fields; containers.Map objects, which store data in key/value pairs; 28and cell arrays, where each element of the array can have a different data type 29and or shape. Multiple input arguments and return values of functions are 30organized as another data container, the comma separated list. 31 32@menu 33* Structures:: 34* containers.Map:: 35* Cell Arrays:: 36* Comma Separated Lists:: 37@end menu 38 39@node Structures 40@section Structures 41@cindex structures 42@cindex data structures 43 44Octave includes support for organizing data in structures. The current 45implementation uses an associative array with indices limited to 46strings, but the syntax is more like C-style structures. 47 48@menu 49* Basic Usage and Examples:: 50* Structure Arrays:: 51* Creating Structures:: 52* Manipulating Structures:: 53* Processing Data in Structures:: 54@end menu 55 56@node Basic Usage and Examples 57@subsection Basic Usage and Examples 58 59Here are some examples of using data structures in Octave. 60 61Elements of structures can be of any value type. For example, the three 62expressions 63 64@example 65@group 66x.a = 1; 67x.b = [1, 2; 3, 4]; 68x.c = "string"; 69@end group 70@end example 71 72@opindex @code{.} structure field access 73@noindent 74create a structure with three elements. The @samp{.} character separates 75the structure name (in the example above @code{x}) from the field name and 76indicates to Octave that this variable is a structure. To print the value 77of the structure you can type its name, just as for any other variable: 78 79@example 80@group 81x 82 @result{} x = 83 84 scalar structure containing the fields: 85 86 a = 1 87 b = 88 89 1 2 90 3 4 91 92 c = string 93@end group 94@end example 95 96@noindent 97Note that Octave may print the elements in any order. 98 99Structures may be copied just like any other variable: 100 101@example 102@group 103y = x 104 @result{} y = 105 106 scalar structure containing the fields: 107 108 a = 1 109 b = 110 111 1 2 112 3 4 113 114 c = string 115@end group 116@end example 117 118Since structures are themselves values, structure elements may reference 119other structures, as well. The following statement adds the field @code{d} 120to the structure @code{x}. The value of field @code{d} is itself a data 121structure containing the single field @code{a}, which has a value of 3. 122 123@example 124x.d.a = 3; 125x.d 126 @result{} ans = 127 128 scalar structure containing the fields: 129 130 a = 3 131 132x 133 @result{} x = 134 135 scalar structure containing the fields: 136 137 a = 1 138 b = 139 140 1 2 141 3 4 142 143 c = string 144 d = 145 146 scalar structure containing the fields: 147 148 a = 3 149@end example 150 151Note that when Octave prints the value of a structure that contains 152other structures, only a few levels are displayed. For example: 153 154@example 155@group 156a.b.c.d.e = 1; 157a 158 @result{} a = 159 160 scalar structure containing the fields: 161 162 b = 163 164 scalar structure containing the fields: 165 166 c = 167 168 scalar structure containing the fields: 169 170 d: 1x1 scalar struct 171@end group 172@end example 173 174@noindent 175This prevents long and confusing output from large deeply nested 176structures. The number of levels to print for nested structures may be 177set with the function @code{struct_levels_to_print}, and the function 178@code{print_struct_array_contents} may be used to enable printing of the 179contents of structure arrays. 180 181@c struct_levels_to_print libinterp/octave-value/ov-struct.cc 182@anchor{XREFstruct_levels_to_print} 183@deftypefn {} {@var{val} =} struct_levels_to_print () 184@deftypefnx {} {@var{old_val} =} struct_levels_to_print (@var{new_val}) 185@deftypefnx {} {} struct_levels_to_print (@var{new_val}, "local") 186Query or set the internal variable that specifies the number of 187structure levels to display. 188 189When called from inside a function with the @qcode{"local"} option, the 190variable is changed locally for the function and any subroutines it calls. 191The original variable value is restored when exiting the function. 192@xseealso{@ref{XREFprint_struct_array_contents,,print_struct_array_contents}} 193@end deftypefn 194 195 196@c print_struct_array_contents libinterp/octave-value/ov-struct.cc 197@anchor{XREFprint_struct_array_contents} 198@deftypefn {} {@var{val} =} print_struct_array_contents () 199@deftypefnx {} {@var{old_val} =} print_struct_array_contents (@var{new_val}) 200@deftypefnx {} {} print_struct_array_contents (@var{new_val}, "local") 201Query or set the internal variable that specifies whether to print struct 202array contents. 203 204If true, values of struct array elements are printed. This variable does 205not affect scalar structures whose elements are always printed. In both 206cases, however, printing will be limited to the number of levels specified 207by @var{struct_levels_to_print}. 208 209When called from inside a function with the @qcode{"local"} option, the 210variable is changed locally for the function and any subroutines it calls. 211The original variable value is restored when exiting the function. 212@xseealso{@ref{XREFstruct_levels_to_print,,struct_levels_to_print}} 213@end deftypefn 214 215 216Functions can return structures. For example, the following function 217separates the real and complex parts of a matrix and stores them in two 218elements of the same structure variable @code{y}. 219 220@example 221@group 222function y = f (x) 223 y.re = real (x); 224 y.im = imag (x); 225endfunction 226@end group 227@end example 228 229When called with a complex-valued argument, the function @code{f} returns 230the data structure containing the real and imaginary parts of the original 231function argument. 232 233@example 234@group 235f (rand (2) + rand (2) * I) 236 @result{} ans = 237 238 scalar structure containing the fields: 239 240 re = 241 242 0.040239 0.242160 243 0.238081 0.402523 244 245 im = 246 247 0.26475 0.14828 248 0.18436 0.83669 249@end group 250@end example 251 252Function return lists can include structure elements, and they may be 253indexed like any other variable. For example: 254 255@example 256[ x.u, x.s(2:3,2:3), x.v ] = svd ([1, 2; 3, 4]); 257x 258 259 @result{} x = 260 261 scalar structure containing the fields: 262 263 u = 264 265 -0.40455 -0.91451 266 -0.91451 0.40455 267 268 s = 269 270 0.00000 0.00000 0.00000 271 0.00000 5.46499 0.00000 272 0.00000 0.00000 0.36597 273 274 v = 275 276 -0.57605 0.81742 277 -0.81742 -0.57605 278@end example 279 280It is also possible to cycle through all the elements of a structure in 281a loop, using a special form of the @code{for} statement 282(@pxref{Looping Over Structure Elements}). 283 284@node Structure Arrays 285@subsection Structure Arrays 286 287A structure array is a particular instance of a structure, where each of 288the fields of the structure is represented by a cell array. Each of 289these cell arrays has the same dimensions. Conceptually, a structure 290array can also be seen as an array of structures with identical 291fields. An example of the creation of a structure array is 292 293@example 294@group 295x(1).a = "string1"; 296x(2).a = "string2"; 297x(1).b = 1; 298x(2).b = 2; 299@end group 300@end example 301 302@noindent 303which creates a 1-by-2 structure array with two fields. Another way 304to create a structure array is with the @code{struct} function 305(@pxref{Creating Structures}). As previously, to print the value of 306the structure array, you can type its name: 307 308@example 309@group 310x 311 @result{} x = 312 @{ 313 1x2 struct array containing the fields: 314 315 a 316 b 317 @} 318@end group 319@end example 320 321Individual elements of the structure array can be returned by indexing 322the variable like @code{@var{x}(1)}, which returns a structure with 323two fields: 324 325@example 326@group 327x(1) 328 @result{} ans = 329 @{ 330 a = string1 331 b = 1 332 @} 333@end group 334@end example 335 336Furthermore, the structure array can return a comma separated list of 337field values (@pxref{Comma Separated Lists}), if indexed by one of its 338own field names. For example: 339 340@example 341@group 342x.a 343 @result{} 344 ans = string1 345 ans = string2 346@end group 347@end example 348 349Here is another example, using this comma separated list on the 350left-hand side of an assignment: 351 352@example 353@group 354[x.a] = deal ("new string1", "new string2"); 355 x(1).a 356 @result{} ans = new string1 357 x(2).a 358 @result{} ans = new string2 359@end group 360@end example 361 362Just as for numerical arrays, it is possible to use vectors as indices 363(@pxref{Index Expressions}): 364 365@example 366@group 367x(3:4) = x(1:2); 368[x([1,3]).a] = deal ("other string1", "other string2"); 369x.a 370 @result{} 371 ans = other string1 372 ans = new string2 373 ans = other string2 374 ans = new string2 375@end group 376@end example 377 378The function @code{size} will return the size of the structure. For 379the example above 380 381@example 382@group 383size (x) 384 @result{} ans = 385 386 1 4 387@end group 388@end example 389 390Elements can be deleted from a structure array in a similar manner to a 391numerical array, by assigning the elements to an empty matrix. For 392example 393 394@example 395@group 396in = struct ("call1", @{x, Inf, "last"@}, 397 "call2", @{x, Inf, "first"@}) 398 @result{} in = 399 @{ 400 1x3 struct array containing the fields: 401 402 call1 403 call2 404 @} 405 406in(1) = []; 407in.call1 408 @result{} 409 ans = Inf 410 ans = last 411@end group 412@end example 413 414@node Creating Structures 415@subsection Creating Structures 416@cindex dynamic naming 417 418Besides the index operator @qcode{"."}, Octave can use dynamic naming 419@qcode{"(var)"} or the @code{struct} function to create structures. Dynamic 420naming uses the string value of a variable as the field name. For example: 421 422@example 423@group 424a = "field2"; 425x.a = 1; 426x.(a) = 2; 427x 428 @result{} x = 429 @{ 430 a = 1 431 field2 = 2 432 @} 433@end group 434@end example 435 436@noindent 437Dynamic indexing also allows you to use arbitrary strings, not merely 438valid Octave identifiers (note that this does not work on @sc{matlab}): 439 440@example 441@group 442a = "long field with spaces (and funny char$)"; 443x.a = 1; 444x.(a) = 2; 445x 446 @result{} x = 447 @{ 448 a = 1 449 long field with spaces (and funny char$) = 2 450 @} 451@end group 452@end example 453 454@noindent 455The warning id @code{Octave:language-extension} can be enabled to warn 456about this usage. @xref{XREFwarning_ids,,warning_ids}. 457 458More realistically, all of the functions that operate on strings can be used 459to build the correct field name before it is entered into the data structure. 460 461@example 462@group 463names = ["Bill"; "Mary"; "John"]; 464ages = [37; 26; 31]; 465for i = 1:rows (names) 466 database.(names(i,:)) = ages(i); 467endfor 468database 469 @result{} database = 470 @{ 471 Bill = 37 472 Mary = 26 473 John = 31 474 @} 475@end group 476@end example 477 478The third way to create structures is the @code{struct} command. @code{struct} 479takes pairs of arguments, where the first argument in the pair is the fieldname 480to include in the structure and the second is a scalar or cell array, 481representing the values to include in the structure or structure array. For 482example: 483 484@example 485@group 486struct ("field1", 1, "field2", 2) 487@result{} ans = 488 @{ 489 field1 = 1 490 field2 = 2 491 @} 492@end group 493@end example 494 495If the values passed to @code{struct} are a mix of scalar and cell 496arrays, then the scalar arguments are expanded to create a 497structure array with a consistent dimension. For example: 498 499@example 500@group 501s = struct ("field1", @{1, "one"@}, "field2", @{2, "two"@}, 502 "field3", 3); 503s.field1 504 @result{} 505 ans = 1 506 ans = one 507 508s.field2 509 @result{} 510 ans = 2 511 ans = two 512 513s.field3 514 @result{} 515 ans = 3 516 ans = 3 517@end group 518@end example 519 520If you want to create a struct which contains a cell array as an 521individual field, you must wrap it in another cell array as shown in 522the following example: 523 524@example 525@group 526struct ("field1", @{@{1, "one"@}@}, "field2", 2) 527 @result{} ans = 528 @{ 529 field1 = 530 531 @{ 532 [1,1] = 1 533 [1,2] = one 534 @} 535 536 field2 = 2 537 @} 538@end group 539@end example 540 541@c struct libinterp/octave-value/ov-struct.cc 542@anchor{XREFstruct} 543@deftypefn {} {@var{s} =} struct () 544@deftypefnx {} {@var{s} =} struct (@var{field1}, @var{value1}, @var{field2}, @var{value2}, @dots{}) 545@deftypefnx {} {@var{s} =} struct (@var{obj}) 546 547Create a scalar or array structure and initialize its values. 548 549The @var{field1}, @var{field2}, @dots{} variables are strings specifying the 550names of the fields and the @var{value1}, @var{value2}, @dots{} variables 551can be of any type. 552 553If the values are cell arrays, create a structure array and initialize its 554values. The dimensions of each cell array of values must match. Singleton 555cells and non-cell values are repeated so that they fill the entire array. 556If the cells are empty, create an empty structure array with the specified 557field names. 558 559If the argument is an object, return the underlying struct. 560 561Observe that the syntax is optimized for struct @strong{arrays}. Consider 562the following examples: 563 564@example 565@group 566struct ("foo", 1) 567 @result{} scalar structure containing the fields: 568 foo = 1 569 570struct ("foo", @{@}) 571 @result{} 0x0 struct array containing the fields: 572 foo 573 574struct ("foo", @{ @{@} @}) 575 @result{} scalar structure containing the fields: 576 foo = @{@}(0x0) 577 578struct ("foo", @{1, 2, 3@}) 579 @result{} 1x3 struct array containing the fields: 580 foo 581 582@end group 583@end example 584 585@noindent 586The first case is an ordinary scalar struct---one field, one value. The 587second produces an empty struct array with one field and no values, since 588being passed an empty cell array of struct array values. When the value is 589a cell array containing a single entry, this becomes a scalar struct with 590that single entry as the value of the field. That single entry happens 591to be an empty cell array. 592 593Finally, if the value is a non-scalar cell array, then @code{struct} 594produces a struct @strong{array}. 595@xseealso{@ref{XREFcell2struct,,cell2struct}, @ref{XREFfieldnames,,fieldnames}, @ref{XREFgetfield,,getfield}, @ref{XREFsetfield,,setfield}, @ref{XREFrmfield,,rmfield}, @ref{XREFisfield,,isfield}, @ref{XREForderfields,,orderfields}, @ref{XREFisstruct,,isstruct}, @ref{XREFstructfun,,structfun}} 596@end deftypefn 597 598 599The function @code{isstruct} can be used to test if an object is a 600structure or a structure array. 601 602@c isstruct libinterp/octave-value/ov-struct.cc 603@anchor{XREFisstruct} 604@deftypefn {} {} isstruct (@var{x}) 605Return true if @var{x} is a structure or a structure array. 606@xseealso{@ref{XREFismatrix,,ismatrix}, @ref{XREFiscell,,iscell}, @ref{XREFisa,,isa}} 607@end deftypefn 608 609 610@node Manipulating Structures 611@subsection Manipulating Structures 612 613Other functions that can manipulate the fields of a structure are given below. 614 615@c numfields libinterp/octave-value/ov-struct.cc 616@anchor{XREFnumfields} 617@deftypefn {} {} numfields (@var{s}) 618Return the number of fields of the structure @var{s}. 619@xseealso{@ref{XREFfieldnames,,fieldnames}} 620@end deftypefn 621 622 623@c fieldnames scripts/miscellaneous/fieldnames.m 624@anchor{XREFfieldnames} 625@deftypefn {} {@var{names} =} fieldnames (@var{struct}) 626@deftypefnx {} {@var{names} =} fieldnames (@var{obj}) 627@deftypefnx {} {@var{names} =} fieldnames (@var{javaobj}) 628@deftypefnx {} {@var{names} =} fieldnames ("@var{javaclassname}") 629Return a cell array of strings with the names of the fields in the 630specified input. 631 632When the input is a structure @var{struct}, the names are the elements of 633the structure. 634 635When the input is an Octave object @var{obj}, the names are the public 636properties of the object. 637 638When the input is a Java object @var{javaobj} or a string containing the 639name of a Java class @var{javaclassname}, the names are the public fields 640(data members) of the object or class. 641@xseealso{@ref{XREFnumfields,,numfields}, @ref{XREFisfield,,isfield}, @ref{XREForderfields,,orderfields}, @ref{XREFstruct,,struct}, @ref{XREFmethods,,methods}} 642@end deftypefn 643 644 645@c isfield libinterp/octave-value/ov-struct.cc 646@anchor{XREFisfield} 647@deftypefn {} {} isfield (@var{x}, "@var{name}") 648@deftypefnx {} {} isfield (@var{x}, @var{name}) 649Return true if the @var{x} is a structure and it includes an element named 650@var{name}. 651 652If @var{name} is a cell array of strings then a logical array of equal 653dimension is returned. 654@xseealso{@ref{XREFfieldnames,,fieldnames}} 655@end deftypefn 656 657 658@c setfield scripts/miscellaneous/setfield.m 659@anchor{XREFsetfield} 660@deftypefn {} {@var{sout} =} setfield (@var{s}, @var{field}, @var{val}) 661@deftypefnx {} {@var{sout} =} setfield (@var{s}, @var{sidx1}, @var{field1}, @var{fidx1}, @var{sidx2}, @var{field2}, @var{fidx2}, @dots{}, @var{val}) 662 663Return a @emph{copy} of the structure @var{s} with the field member 664@var{field} set to the value @var{val}. 665 666For example: 667 668@example 669@group 670@var{s} = struct (); 671@var{s} = setfield (@var{s}, "foo bar", 42); 672@end group 673@end example 674 675@noindent 676This is equivalent to 677 678@example 679@var{s}.("foo bar") = 42; 680@end example 681 682@noindent 683Note that ordinary structure syntax @code{@var{s}.foo bar = 42} cannot be 684used here, as the field name is not a valid Octave identifier because of 685the space character. Using arbitrary strings for field names is 686incompatible with @sc{matlab}, and this usage will emit a warning if the 687warning ID @code{Octave:language-extension} is enabled. 688@xref{XREFwarning_ids,,warning_ids}. 689 690With the second calling form, set a field of a structure array. The 691input @var{sidx} selects an element of the structure array, @var{field} 692specifies the field name of the selected element, and @var{fidx} selects 693which element of the field (in the case of an array or cell array). 694The @var{sidx}, @var{field}, and @var{fidx} inputs can be repeated to 695address nested structure array elements. The structure array index and 696field element index must be cell arrays while the field name must be a 697string. 698 699For example: 700 701@example 702@group 703@var{s} = struct ("baz", 42); 704setfield (@var{s}, @{1@}, "foo", @{1@}, "bar", 54) 705@result{} 706 ans = 707 scalar structure containing the fields: 708 baz = 42 709 foo = 710 scalar structure containing the fields: 711 bar = 54 712@end group 713@end example 714 715The example begins with an ordinary scalar structure to which a nested 716scalar structure is added. In all cases, if the structure index @var{sidx} 717is not specified it defaults to 1 (scalar structure). Thus, the example 718above could be written more concisely as 719@code{setfield (@var{s}, "foo", "bar", 54)} 720 721Finally, an example with nested structure arrays: 722 723@example 724@group 725@var{sa}.foo = 1; 726@var{sa} = setfield (@var{sa}, @{2@}, "bar", @{3@}, "baz", @{1, 4@}, 5); 727@var{sa}(2).bar(3) 728@result{} 729 ans = 730 scalar structure containing the fields: 731 baz = 0 0 0 5 732@end group 733@end example 734 735Here @var{sa} is a structure array whose field at elements 1 and 2 is in 736turn another structure array whose third element is a simple scalar 737structure. The terminal scalar structure has a field which contains a 738matrix value. 739 740Note that the same result as in the above example could be achieved by: 741 742@example 743@group 744@var{sa}.foo = 1; 745@var{sa}(2).bar(3).baz(1,4) = 5 746@end group 747@end example 748@xseealso{@ref{XREFgetfield,,getfield}, @ref{XREFrmfield,,rmfield}, @ref{XREForderfields,,orderfields}, @ref{XREFisfield,,isfield}, @ref{XREFfieldnames,,fieldnames}, @ref{XREFisstruct,,isstruct}, @ref{XREFstruct,,struct}} 749@end deftypefn 750 751 752@c getfield scripts/miscellaneous/getfield.m 753@anchor{XREFgetfield} 754@deftypefn {} {@var{val} =} getfield (@var{s}, @var{field}) 755@deftypefnx {} {@var{val} =} getfield (@var{s}, @var{sidx1}, @var{field1}, @var{fidx1}, @dots{}) 756Get the value of the field named @var{field} from a structure or nested 757structure @var{s}. 758 759If @var{s} is a structure array then @var{sidx} selects an element of the 760structure array, @var{field} specifies the field name of the selected 761element, and @var{fidx} selects which element of the field (in the case of 762an array or cell array). See @code{setfield} for a more complete 763description of the syntax. 764 765@xseealso{@ref{XREFsetfield,,setfield}, @ref{XREFrmfield,,rmfield}, @ref{XREForderfields,,orderfields}, @ref{XREFisfield,,isfield}, @ref{XREFfieldnames,,fieldnames}, @ref{XREFisstruct,,isstruct}, @ref{XREFstruct,,struct}} 766@end deftypefn 767 768 769@c rmfield libinterp/octave-value/ov-struct.cc 770@anchor{XREFrmfield} 771@deftypefn {} {@var{sout} =} rmfield (@var{s}, "@var{f}") 772@deftypefnx {} {@var{sout} =} rmfield (@var{s}, @var{f}) 773Return a @emph{copy} of the structure (array) @var{s} with the field @var{f} 774removed. 775 776If @var{f} is a cell array of strings or a character array, remove each of 777the named fields. 778@xseealso{@ref{XREForderfields,,orderfields}, @ref{XREFfieldnames,,fieldnames}, @ref{XREFisfield,,isfield}} 779@end deftypefn 780 781 782@c orderfields scripts/miscellaneous/orderfields.m 783@anchor{XREForderfields} 784@deftypefn {} {@var{sout} =} orderfields (@var{s1}) 785@deftypefnx {} {@var{sout} =} orderfields (@var{s1}, @var{s2}) 786@deftypefnx {} {@var{sout} =} orderfields (@var{s1}, @{@var{cellstr}@}) 787@deftypefnx {} {@var{sout} =} orderfields (@var{s1}, @var{p}) 788@deftypefnx {} {[@var{sout}, @var{p}] =} orderfields (@dots{}) 789Return a @emph{copy} of @var{s1} with fields arranged alphabetically, or as 790specified by the second input. 791 792Given one input struct @var{s1}, arrange field names alphabetically. 793 794If a second struct argument is given, arrange field names in @var{s1} as 795they appear in @var{s2}. The second argument may also specify the order 796in a cell array of strings @var{cellstr}. The second argument may also 797be a permutation vector. 798 799The optional second output argument @var{p} is the permutation vector which 800converts the original name order to the new name order. 801 802Examples: 803 804@example 805@group 806s = struct ("d", 4, "b", 2, "a", 1, "c", 3); 807t1 = orderfields (s) 808 @result{} t1 = 809 scalar structure containing the fields: 810 a = 1 811 b = 2 812 c = 3 813 d = 4 814@end group 815@end example 816 817@example 818@group 819t = struct ("d", @{@}, "c", @{@}, "b", @{@}, "a", @{@}); 820t2 = orderfields (s, t) 821 @result{} t2 = 822 scalar structure containing the fields: 823 d = 4 824 c = 3 825 b = 2 826 a = 1 827@end group 828@end example 829 830@example 831@group 832t3 = orderfields (s, [3, 2, 4, 1]) 833 @result{} t3 = 834 scalar structure containing the fields: 835 a = 1 836 b = 2 837 c = 3 838 d = 4 839@end group 840@end example 841 842@example 843@group 844[t4, p] = orderfields (s, @{"d", "c", "b", "a"@}) 845 @result{} t4 = 846 scalar structure containing the fields: 847 d = 4 848 c = 3 849 b = 2 850 a = 1 851 p = 852 1 853 4 854 2 855 3 856@end group 857@end example 858 859@xseealso{@ref{XREFfieldnames,,fieldnames}, @ref{XREFgetfield,,getfield}, @ref{XREFsetfield,,setfield}, @ref{XREFrmfield,,rmfield}, @ref{XREFisfield,,isfield}, @ref{XREFisstruct,,isstruct}, @ref{XREFstruct,,struct}} 860@end deftypefn 861 862 863@c substruct scripts/miscellaneous/substruct.m 864@anchor{XREFsubstruct} 865@deftypefn {} {} substruct (@var{type}, @var{subs}, @dots{}) 866Create a subscript structure for use with @code{subsref} or @code{subsasgn}. 867 868For example: 869 870@example 871@group 872idx = substruct ("()", @{3, ":"@}) 873 @result{} idx = 874 scalar structure containing the fields: 875 type = () 876 subs = 877 @{ 878 [1,1] = 3 879 [1,2] = : 880 @} 881x = [1, 2, 3; 882 4, 5, 6; 883 7, 8, 9]; 884subsref (x, idx) 885 @result{} 7 8 9 886@end group 887@end example 888@xseealso{@ref{XREFsubsref,,subsref}, @ref{XREFsubsasgn,,subsasgn}} 889@end deftypefn 890 891 892@node Processing Data in Structures 893@subsection Processing Data in Structures 894 895The simplest way to process data in a structure is within a @code{for} 896loop (@pxref{Looping Over Structure Elements}). A similar effect can be 897achieved with the @code{structfun} function, where a user defined 898function is applied to each field of the structure. 899@xref{XREFstructfun,,structfun}. 900 901Alternatively, to process the data in a structure, the structure might 902be converted to another type of container before being treated. 903 904@c struct2cell libinterp/octave-value/ov-cell.cc 905@anchor{XREFstruct2cell} 906@deftypefn {} {@var{c} =} struct2cell (@var{s}) 907Create a new cell array from the objects stored in the struct object. 908 909If @var{f} is the number of fields in the structure, the resulting cell 910array will have a dimension vector corresponding to 911@code{[@var{f} size(@var{s})]}. For example: 912 913@example 914@group 915s = struct ("name", @{"Peter", "Hannah", "Robert"@}, 916 "age", @{23, 16, 3@}); 917c = struct2cell (s) 918 @result{} c = @{2x1x3 Cell Array@} 919c(1,1,:)(:) 920 @result{} 921 @{ 922 [1,1] = Peter 923 [2,1] = Hannah 924 [3,1] = Robert 925 @} 926c(2,1,:)(:) 927 @result{} 928 @{ 929 [1,1] = 23 930 [2,1] = 16 931 [3,1] = 3 932 @} 933@end group 934@end example 935 936@xseealso{@ref{XREFcell2struct,,cell2struct}, @ref{XREFnamedargs2cell,,namedargs2cell}, @ref{XREFfieldnames,,fieldnames}} 937@end deftypefn 938 939 940@c namedargs2cell scripts/miscellaneous/namedargs2cell.m 941@anchor{XREFnamedargs2cell} 942@deftypefn {} {@var{c} =} namedargs2cell (@var{s}) 943Create a cell array of field name/value pairs from a scalar structure. 944 945Example: 946 947@example 948@group 949@c doctest: +SKIP 950s.Name = "Peter"; 951s.Height = 185; 952s.Age = 42; 953 954c = namedargs2cell (s) 955 @result{} @{ "Name", "Peter", "Height", 185, "Age", 42 @} 956@end group 957@end example 958 959@xseealso{@ref{XREFstruct2cell,,struct2cell}} 960@end deftypefn 961 962 963@node containers.Map 964@section containers.Map 965@cindex Map 966@cindex key/value store 967@cindex hash table 968 969@c FIXME: Need to fill in documentation on what a Map is, when to use it over 970@c other container types, how to perform basic operations with a Map. 971 972@c FIXME: Currently have trouble getting documentation for classdef functions. 973@c containers.Map scripts/+containers/Map.m 974@anchor{XREFcontainers_Map} 975@deftypefn {} {@var{m} =} containers.Map () 976@deftypefnx {} {@var{m} =} containers.Map (@var{keys}, @var{vals}) 977@deftypefnx {} {@var{m} =} containers.Map (@var{keys}, @var{vals}, @qcode{"UniformValues"}, @var{is_uniform}) 978@deftypefnx {} {@var{m} =} containers.Map (@qcode{"KeyType"}, @var{kt}, @qcode{"ValueType"}, @var{vt}) 979 980Create an object of the containers.Map class that stores a list of key/value 981pairs. 982 983@var{keys} is an array of @emph{unique} keys for the map. The keys can be 984numeric scalars or strings. The type for numeric keys may be one of 985@qcode{"double"}, @qcode{"single"}, @qcode{"int32"}, @qcode{"uint32"}, 986@qcode{"int64"}, or @qcode{"uint64"}. Other numeric or logical keys will 987be converted to @qcode{"double"}. A single string key may be entered as is. 988Multiple string keys are entered as a cell array of strings. 989 990@var{vals} is an array of values for the map with the @emph{same} number 991of elements as @var{keys}. 992 993When called with no input arguments a default map is created with strings 994as the key type and @qcode{"any"} as the value type. 995 996The @qcode{"UniformValues"} option specifies whether the values of 997the map must be strictly of the same type. If @var{is_uniform} is true, any 998values which would be added to the map are first validated to ensure they 999are of the correct type. 1000 1001When called with @qcode{"KeyType"} and @qcode{"ValueType"} arguments, create 1002an empty map with the specified types. The inputs @var{kt} and @var{vt} are 1003the types for the keys and values of the map respectively. Allowed values 1004for @var{kt} are @qcode{"char"}, @qcode{"double"}, @qcode{"single"}, 1005@qcode{"int32"}, @qcode{"uint32"}, @qcode{"int64"}, @qcode{"uint64"}. 1006Allowed values for @var{vt} are @qcode{"any"}, @qcode{"char"}, 1007@qcode{"double"}, @qcode{"single"}, @qcode{"int32"}, @qcode{"uint32"}, 1008@qcode{"int64"}, @qcode{"uint64"}, @qcode{"logical"}. 1009 1010The return value @var{m} is an object of the containers.Map class. 1011@xseealso{@ref{XREFstruct,,struct}} 1012@end deftypefn 1013 1014 1015@node Cell Arrays 1016@section Cell Arrays 1017@cindex cell arrays 1018 1019It can be both necessary and convenient to store several variables of 1020different size or type in one variable. A cell array is a container 1021class able to do just that. In general cell arrays work just like 1022@math{N}-dimensional arrays with the exception of the use of @samp{@{} 1023and @samp{@}} as allocation and indexing operators. 1024 1025@menu 1026* Basic Usage of Cell Arrays:: 1027* Creating Cell Arrays:: 1028* Indexing Cell Arrays:: 1029* Cell Arrays of Strings:: 1030* Processing Data in Cell Arrays:: 1031@end menu 1032 1033@node Basic Usage of Cell Arrays 1034@subsection Basic Usage of Cell Arrays 1035@opindex @{ 1036@opindex @} 1037As an example, the following code creates a cell array containing a 1038string and a 2-by-2 random matrix 1039 1040@example 1041c = @{"a string", rand(2, 2)@}; 1042@end example 1043 1044@noindent 1045To access the elements of a cell array, it can be indexed with the @{ 1046and @} operators. Thus, the variable created in the previous example 1047can be indexed like this: 1048 1049@example 1050@group 1051c@{1@} 1052 @result{} ans = a string 1053@end group 1054@end example 1055 1056@noindent 1057As with numerical arrays several elements of a cell array can be 1058extracted by indexing with a vector of indexes 1059 1060@example 1061@group 1062c@{1:2@} 1063 @result{} ans = a string 1064 @result{} ans = 1065 1066 0.593993 0.627732 1067 0.377037 0.033643 1068@end group 1069@end example 1070 1071The indexing operators can also be used to insert or overwrite elements 1072of a cell array. The following code inserts the scalar 3 on the 1073third place of the previously created cell array 1074 1075@example 1076@group 1077c@{3@} = 3 1078 @result{} c = 1079 1080 @{ 1081 [1,1] = a string 1082 [1,2] = 1083 1084 0.593993 0.627732 1085 0.377037 0.033643 1086 1087 [1,3] = 3 1088 @} 1089@end group 1090@end example 1091 1092Details on indexing cell arrays are explained in @ref{Indexing Cell Arrays}. 1093 1094In general nested cell arrays are displayed hierarchically as in the 1095previous example. In some circumstances it makes sense to reference 1096them by their index, and this can be performed by the @code{celldisp} 1097function. 1098 1099@c celldisp scripts/general/celldisp.m 1100@anchor{XREFcelldisp} 1101@deftypefn {} {} celldisp (@var{c}) 1102@deftypefnx {} {} celldisp (@var{c}, @var{name}) 1103Recursively display the contents of a cell array. 1104 1105By default the values are displayed with the name of the variable @var{c}. 1106However, this name can be replaced with the variable @var{name}. For 1107example: 1108 1109@example 1110@group 1111c = @{1, 2, @{31, 32@}@}; 1112celldisp (c, "b") 1113 @result{} 1114 b@{1@} = 1115 1 1116 b@{2@} = 1117 2 1118 b@{3@}@{1@} = 1119 31 1120 b@{3@}@{2@} = 1121 32 1122@end group 1123@end example 1124 1125@xseealso{@ref{XREFdisp,,disp}} 1126@end deftypefn 1127 1128 1129To test if an object is a cell array, use the @code{iscell} 1130function. For example: 1131 1132@example 1133@group 1134iscell (c) 1135 @result{} ans = 1 1136 1137iscell (3) 1138 @result{} ans = 0 1139 1140@end group 1141@end example 1142 1143@c iscell libinterp/octave-value/ov-cell.cc 1144@anchor{XREFiscell} 1145@deftypefn {} {} iscell (@var{x}) 1146Return true if @var{x} is a cell array object. 1147@xseealso{@ref{XREFismatrix,,ismatrix}, @ref{XREFisstruct,,isstruct}, @ref{XREFiscellstr,,iscellstr}, @ref{XREFisa,,isa}} 1148@end deftypefn 1149 1150 1151@node Creating Cell Arrays 1152@subsection Creating Cell Arrays 1153 1154The introductory example (@pxref{Basic Usage of Cell Arrays}) showed 1155how to create a cell array containing currently available variables. 1156In many situations, however, it is useful to create a cell array and 1157then fill it with data. 1158 1159The @code{cell} function returns a cell array of a given size, containing 1160empty matrices. This function is similar to the @code{zeros} 1161function for creating new numerical arrays. The following example creates 1162a 2-by-2 cell array containing empty matrices 1163 1164@example 1165@group 1166c = cell (2,2) 1167 @result{} c = 1168 1169 @{ 1170 [1,1] = [](0x0) 1171 [2,1] = [](0x0) 1172 [1,2] = [](0x0) 1173 [2,2] = [](0x0) 1174 @} 1175@end group 1176@end example 1177 1178Just like numerical arrays, cell arrays can be multi-dimensional. The 1179@code{cell} function accepts any number of positive integers to describe 1180the size of the returned cell array. It is also possible to set the size 1181of the cell array through a vector of positive integers. In the 1182following example two cell arrays of equal size are created, and the size 1183of the first one is displayed 1184 1185@example 1186@group 1187c1 = cell (3, 4, 5); 1188c2 = cell ( [3, 4, 5] ); 1189size (c1) 1190 @result{} ans = 1191 3 4 5 1192@end group 1193@end example 1194 1195@noindent 1196As can be seen, the @ref{XREFsize,,size} function also works 1197for cell arrays. As do other functions describing the size of an 1198object, such as @ref{XREFlength,,length}, @ref{XREFnumel,, numel}, 1199@ref{XREFrows,,rows}, and @ref{XREFcolumns,,columns}. 1200 1201@c cell libinterp/octave-value/ov-cell.cc 1202@anchor{XREFcell} 1203@deftypefn {} {} cell (@var{n}) 1204@deftypefnx {} {} cell (@var{m}, @var{n}) 1205@deftypefnx {} {} cell (@var{m}, @var{n}, @var{k}, @dots{}) 1206@deftypefnx {} {} cell ([@var{m} @var{n} @dots{}]) 1207Create a new cell array object. 1208 1209If invoked with a single scalar integer argument, return a square 1210@nospell{NxN} cell array. If invoked with two or more scalar integer 1211arguments, or a vector of integer values, return an array with the given 1212dimensions. 1213@xseealso{@ref{XREFcellstr,,cellstr}, @ref{XREFmat2cell,,mat2cell}, @ref{XREFnum2cell,,num2cell}, @ref{XREFstruct2cell,,struct2cell}} 1214@end deftypefn 1215 1216 1217As an alternative to creating empty cell arrays, and then filling them, it 1218is possible to convert numerical arrays into cell arrays using the 1219@code{num2cell}, @code{mat2cell} and @code{cellslices} functions. 1220 1221@c num2cell libinterp/corefcn/cellfun.cc 1222@anchor{XREFnum2cell} 1223@deftypefn {} {@var{C} =} num2cell (@var{A}) 1224@deftypefnx {} {@var{C} =} num2cell (@var{A}, @var{dim}) 1225Convert the numeric matrix @var{A} to a cell array. 1226 1227When no @var{dim} is specified, each element of @var{A} becomes a 1x1 element 1228in the output @var{C}. 1229 1230If @var{dim} is defined then individual elements of @var{C} contain all of the 1231elements from @var{A} along the specified dimension. @var{dim} may also be a 1232vector of dimensions with the same rule applied. 1233 1234For example: 1235 1236@example 1237x = [1,2;3,4] 1238@result{} 1239 1 2 1240 3 4 1241 1242## each element of A becomes a 1x1 element of C 1243num2cell (x) 1244 @result{} 1245 @{ 1246 [1,1] = 1 1247 [2,1] = 3 1248 [1,2] = 2 1249 [2,2] = 4 1250 @} 1251## all rows (dim 1) of A appear in each element of C 1252num2cell (x, 1) 1253 @result{} 1254 @{ 1255 [1,1] = 1256 1 1257 3 1258 [1,2] = 1259 2 1260 4 1261 @} 1262## all columns (dim 2) of A appear in each element of C 1263num2cell (x, 2) 1264 @result{} 1265 @{ 1266 [1,1] = 1267 1 2 1268 [2,1] = 1269 3 4 1270 @} 1271## all rows and cols appear in each element of C 1272## (hence, only 1 output) 1273num2cell (x, [1, 2]) 1274 @result{} 1275 @{ 1276 [1,1] = 1277 1 2 1278 3 4 1279 @} 1280@end example 1281 1282@xseealso{@ref{XREFmat2cell,,mat2cell}} 1283@end deftypefn 1284 1285 1286@c mat2cell libinterp/corefcn/cellfun.cc 1287@anchor{XREFmat2cell} 1288@deftypefn {} {@var{C} =} mat2cell (@var{A}, @var{dim1}, @var{dim2}, @dots{}, @var{dimi}, @dots{}, @var{dimn}) 1289@deftypefnx {} {@var{C} =} mat2cell (@var{A}, @var{rowdim}) 1290Convert the matrix @var{A} to a cell array. 1291 1292Each dimension argument (@var{dim1}, @var{dim2}, etc.@:) is a vector of 1293integers which specifies how to divide that dimension's elements amongst the 1294new elements in the output @var{C}. The number of elements in the @var{i}-th 1295dimension is @code{size (@var{A}, @var{i})}. Because all elements in @var{A} 1296must be partitioned, there is a requirement that @code{sum (@var{dimi}) == size 1297(@var{A}, i)}. The size of the output cell @var{C} is numel (@var{dim1}) x 1298numel (@var{dim2}) x @dots{} x numel (@var{dimn}). 1299 1300Given a single dimensional argument, @var{rowdim}, the output is divided into 1301rows as specified. All other dimensions are not divided and thus all 1302columns (dim 2), pages (dim 3), etc.@: appear in each output element. 1303 1304Examples 1305 1306@example 1307x = reshape (1:12, [3, 4])' 1308@result{} 1309 1 2 3 1310 4 5 6 1311 7 8 9 1312 10 11 12 1313 1314@group 1315## The 4 rows (dim1) are divided in to two cell elements 1316## with 2 rows each. 1317## The 3 cols (dim2) are divided in to three cell elements 1318## with 1 col each. 1319mat2cell (x, [2,2], [1,1,1]) 1320@result{} 1321@{ 1322 [1,1] = 1323 1324 1 1325 4 1326 1327 [2,1] = 1328 1329 7 1330 10 1331 1332 [1,2] = 1333 1334 2 1335 5 1336 1337 [2,2] = 1338 8 1339 11 1340 1341 [1,3] = 1342 1343 3 1344 6 1345 1346 [2,3] = 1347 9 1348 12 1349@} 1350@end group 1351 1352@group 1353## The 4 rows (dim1) are divided in to two cell elements 1354## with a 3/1 split. 1355## All columns appear in each output element. 1356mat2cell (x, [3,1]) 1357@result{} 1358@{ 1359 [1,1] = 1360 1361 1 2 3 1362 4 5 6 1363 7 8 9 1364 1365 [2,1] = 1366 1367 10 11 12 1368@} 1369@end group 1370@end example 1371 1372@xseealso{@ref{XREFnum2cell,,num2cell}, @ref{XREFcell2mat,,cell2mat}} 1373@end deftypefn 1374 1375 1376@c cellslices libinterp/corefcn/cellfun.cc 1377@anchor{XREFcellslices} 1378@deftypefn {} {@var{sl} =} cellslices (@var{x}, @var{lb}, @var{ub}, @var{dim}) 1379Given an array @var{x}, this function produces a cell array of slices from 1380the array determined by the index vectors @var{lb}, @var{ub}, for lower and 1381upper bounds, respectively. 1382 1383In other words, it is equivalent to the following code: 1384 1385@example 1386@group 1387n = length (lb); 1388sl = cell (1, n); 1389for i = 1:length (lb) 1390 sl@{i@} = x(:,@dots{},lb(i):ub(i),@dots{},:); 1391endfor 1392@end group 1393@end example 1394 1395The position of the index is determined by @var{dim}. If not specified, 1396slicing is done along the first non-singleton dimension. 1397@xseealso{@ref{XREFcell2mat,,cell2mat}, @ref{XREFcellindexmat,,cellindexmat}, @ref{XREFcellfun,,cellfun}} 1398@end deftypefn 1399 1400 1401@node Indexing Cell Arrays 1402@subsection Indexing Cell Arrays 1403 1404As shown in @pxref{Basic Usage of Cell Arrays} elements can be 1405extracted from cell arrays using the @samp{@{} and @samp{@}} 1406operators. If you want to extract or access subarrays which are still 1407cell arrays, you need to use the @samp{(} and @samp{)} operators. The 1408following example illustrates the difference: 1409 1410@example 1411@group 1412c = @{"1", "2", "3"; "x", "y", "z"; "4", "5", "6"@}; 1413c@{2,3@} 1414 @result{} ans = z 1415 1416c(2,3) 1417 @result{} ans = 1418 @{ 1419 [1,1] = z 1420 @} 1421@end group 1422@end example 1423 1424@noindent So with @samp{@{@}} you access elements of a cell 1425array, while with @samp{()} you access a sub array of a cell 1426array. 1427 1428Using the @samp{(} and @samp{)} operators, indexing works for cell 1429arrays like for multi-dimensional arrays. As an example, all the rows 1430of the first and third column of a cell array can be set to @code{0} 1431with the following command: 1432 1433@example 1434@group 1435c(:, [1, 3]) = @{0@} 1436 @result{} = 1437 @{ 1438 [1,1] = 0 1439 [2,1] = 0 1440 [3,1] = 0 1441 [1,2] = 2 1442 [2,2] = y 1443 [3,2] = 5 1444 [1,3] = 0 1445 [2,3] = 0 1446 [3,3] = 0 1447 @} 1448@end group 1449@end example 1450 1451Note, that the above can also be achieved like this: 1452 1453@example 1454c(:, [1, 3]) = 0; 1455@end example 1456 1457@noindent Here, the scalar @samp{0} is automatically promoted to 1458cell array @samp{@{0@}} and then assigned to the subarray of @code{c}. 1459 1460To give another example for indexing cell arrays with @samp{()}, you 1461can exchange the first and the second row of a cell array as in the 1462following command: 1463 1464@example 1465@group 1466c = @{1, 2, 3; 4, 5, 6@}; 1467c([1, 2], :) = c([2, 1], :) 1468 @result{} = 1469 @{ 1470 [1,1] = 4 1471 [2,1] = 1 1472 [1,2] = 5 1473 [2,2] = 2 1474 [1,3] = 6 1475 [2,3] = 3 1476 @} 1477@end group 1478@end example 1479 1480Accessing multiple elements of a cell array with the @samp{@{} and 1481@samp{@}} operators will result in a comma-separated list of all the 1482requested elements (@pxref{Comma Separated Lists}). Using the 1483@samp{@{} and @samp{@}} operators the first two rows in the above 1484example can be swapped back like this: 1485 1486@example 1487@group 1488[c@{[1,2], :@}] = deal (c@{[2, 1], :@}) 1489 @result{} = 1490 @{ 1491 [1,1] = 1 1492 [2,1] = 4 1493 [1,2] = 2 1494 [2,2] = 5 1495 [1,3] = 3 1496 [2,3] = 6 1497 @} 1498@end group 1499@end example 1500 1501As for struct arrays and numerical arrays, the empty matrix @samp{[]} 1502can be used to delete elements from a cell array: 1503 1504@example 1505@group 1506x = @{"1", "2"; "3", "4"@}; 1507x(1, :) = [] 1508 @result{} x = 1509 @{ 1510 [1,1] = 3 1511 [1,2] = 4 1512 @} 1513@end group 1514@end example 1515 1516The following example shows how to just remove the contents of cell 1517array elements but not delete the space for them: 1518 1519@example 1520@group 1521x = @{"1", "2"; "3", "4"@}; 1522x(1, :) = @{[]@} 1523@result{} x = 1524 @{ 1525 [1,1] = [](0x0) 1526 [2,1] = 3 1527 [1,2] = [](0x0) 1528 [2,2] = 4 1529 @} 1530@end group 1531@end example 1532 1533The indexing operations operate on the cell array and not on the objects 1534within the cell array. By contrast, @code{cellindexmat} applies matrix 1535indexing to the objects within each cell array entry and returns the requested 1536values. 1537 1538@c cellindexmat libinterp/corefcn/cellfun.cc 1539@anchor{XREFcellindexmat} 1540@deftypefn {} {@var{y} =} cellindexmat (@var{x}, @var{varargin}) 1541Perform indexing of matrices in a cell array. 1542 1543Given a cell array of matrices @var{x}, this function computes 1544 1545@example 1546@group 1547Y = cell (size (X)); 1548for i = 1:numel (X) 1549 Y@{i@} = X@{i@}(varargin@{1@}, varargin@{2@}, @dots{}, varargin@{N@}); 1550endfor 1551@end group 1552@end example 1553 1554The indexing arguments may be scalar (@code{2}), arrays (@code{[1, 3]}), 1555ranges (@code{1:3}), or the colon operator (@qcode{":"}). However, the 1556indexing keyword @code{end} is not available. 1557@xseealso{@ref{XREFcellslices,,cellslices}, @ref{XREFcellfun,,cellfun}} 1558@end deftypefn 1559 1560 1561@node Cell Arrays of Strings 1562@subsection Cell Arrays of Strings 1563 1564One common use of cell arrays is to store multiple strings in the same 1565variable. It is also possible to store multiple strings in a 1566character matrix by letting each row be a string. This, however, 1567introduces the problem that all strings must be of equal length. 1568Therefore, it is recommended to use cell arrays to store multiple 1569strings. For cases, where the character matrix representation is required 1570for an operation, there are several functions that convert a cell 1571array of strings to a character array and back. @code{char} and 1572@code{strvcat} convert cell arrays to a character array 1573(@pxref{Concatenating Strings}), while the function @code{cellstr} 1574converts a character array to a cell array of strings: 1575 1576@example 1577@group 1578a = ["hello"; "world"]; 1579c = cellstr (a) 1580 @result{} c = 1581 @{ 1582 [1,1] = hello 1583 [2,1] = world 1584 @} 1585@end group 1586@end example 1587 1588@c cellstr libinterp/octave-value/ov-cell.cc 1589@anchor{XREFcellstr} 1590@deftypefn {} {@var{cstr} =} cellstr (@var{strmat}) 1591Create a new cell array object from the elements of the string array 1592@var{strmat}. 1593 1594Each row of @var{strmat} becomes an element of @var{cstr}. Any trailing 1595spaces in a row are deleted before conversion. 1596 1597To convert back from a cellstr to a character array use @code{char}. 1598@xseealso{@ref{XREFcell,,cell}, @ref{XREFchar,,char}} 1599@end deftypefn 1600 1601 1602One further advantage of using cell arrays to store multiple strings is 1603that most functions for string manipulations included with Octave 1604support this representation. As an example, it is possible to compare 1605one string with many others using the @code{strcmp} function. If one of 1606the arguments to this function is a string and the other is a cell array 1607of strings, each element of the cell array will be compared to the string 1608argument: 1609 1610@example 1611@group 1612c = @{"hello", "world"@}; 1613strcmp ("hello", c) 1614 @result{} ans = 1615 1 0 1616@end group 1617@end example 1618 1619@noindent 1620The following string functions support cell arrays of strings: 1621@code{char}, @code{strvcat}, @code{strcat} (@pxref{Concatenating 1622Strings}), @code{strcmp}, @code{strncmp}, @code{strcmpi}, 1623@code{strncmpi} (@pxref{Comparing Strings}), @code{str2double}, 1624@code{deblank}, @code{strtrim}, @code{strtrunc}, @code{strfind}, 1625@code{strmatch}, , @code{regexp}, @code{regexpi} 1626(@pxref{Manipulating Strings}) and @code{str2double} 1627(@pxref{String Conversions}). 1628 1629The function @code{iscellstr} can be used to test if an object is a 1630cell array of strings. 1631 1632@c iscellstr libinterp/octave-value/ov-cell.cc 1633@anchor{XREFiscellstr} 1634@deftypefn {} {} iscellstr (@var{cell}) 1635Return true if every element of the cell array @var{cell} is a character 1636string. 1637@xseealso{@ref{XREFischar,,ischar}, @ref{XREFisstring,,isstring}} 1638@end deftypefn 1639 1640 1641@node Processing Data in Cell Arrays 1642@subsection Processing Data in Cell Arrays 1643 1644Data that is stored in a cell array can be processed in several ways 1645depending on the actual data. The simplest way to process that data 1646is to iterate through it using one or more @code{for} loops. The same 1647idea can be implemented more easily through the use of the @code{cellfun} 1648function that calls a user-specified function on all elements of a cell 1649array. @xref{XREFcellfun,,cellfun}. 1650 1651An alternative is to convert the data to a different container, such as 1652a matrix or a data structure. Depending on the data this is possible 1653using the @code{cell2mat} and @code{cell2struct} functions. 1654 1655@c cell2mat scripts/general/cell2mat.m 1656@anchor{XREFcell2mat} 1657@deftypefn {} {@var{m} =} cell2mat (@var{c}) 1658Convert the cell array @var{c} into a matrix by concatenating all 1659elements of @var{c} into a hyperrectangle. 1660 1661Elements of @var{c} must be numeric, logical, or char matrices; or cell 1662arrays; or structs; and @code{cat} must be able to concatenate them 1663together. 1664@xseealso{@ref{XREFmat2cell,,mat2cell}, @ref{XREFnum2cell,,num2cell}} 1665@end deftypefn 1666 1667 1668@c cell2struct libinterp/octave-value/ov-struct.cc 1669@anchor{XREFcell2struct} 1670@deftypefn {} {} cell2struct (@var{cell}, @var{fields}) 1671@deftypefnx {} {} cell2struct (@var{cell}, @var{fields}, @var{dim}) 1672Convert @var{cell} to a structure. 1673 1674The number of fields in @var{fields} must match the number of elements in 1675@var{cell} along dimension @var{dim}, that is 1676@code{numel (@var{fields}) == size (@var{cell}, @var{dim})}. If @var{dim} 1677is omitted, a value of 1 is assumed. 1678 1679@example 1680@group 1681A = cell2struct (@{"Peter", "Hannah", "Robert"; 1682 185, 170, 168@}, 1683 @{"Name","Height"@}, 1); 1684A(1) 1685 @result{} 1686 @{ 1687 Name = Peter 1688 Height = 185 1689 @} 1690 1691@end group 1692@end example 1693@xseealso{@ref{XREFstruct2cell,,struct2cell}, @ref{XREFcell2mat,,cell2mat}, @ref{XREFstruct,,struct}} 1694@end deftypefn 1695 1696 1697@node Comma Separated Lists 1698@section Comma Separated Lists 1699@cindex comma separated lists 1700@cindex cs-lists 1701 1702Comma separated lists @footnote{Comma-separated lists are also sometimes 1703informally referred to as @dfn{cs-lists}.} are the basic argument type 1704to all Octave functions - both for input and return arguments. In the 1705example 1706 1707@example 1708max (@var{a}, @var{b}) 1709@end example 1710 1711@noindent 1712@samp{@var{a}, @var{b}} is a comma separated list. Comma separated lists 1713can appear on both the right and left hand side of an assignment. For 1714example 1715 1716@example 1717@group 1718x = [1 0 1 0 0 1 1; 0 0 0 0 0 0 7]; 1719[@var{i}, @var{j}] = find (@var{x}, 2, "last"); 1720@end group 1721@end example 1722 1723@noindent 1724Here, @samp{@var{x}, 2, "last"} is a comma separated list constituting 1725the input arguments of @code{find}. @code{find} returns a comma 1726separated list of output arguments which is assigned element by 1727element to the comma separated list @samp{@var{i}, @var{j}}. 1728 1729Another example of where comma separated lists are used is in the 1730creation of a new array with @code{[]} (@pxref{Matrices}) or the 1731creation of a cell array with @code{@{@}} (@pxref{Basic Usage of Cell 1732Arrays}). In the expressions 1733 1734@example 1735@group 1736a = [1, 2, 3, 4]; 1737c = @{4, 5, 6, 7@}; 1738@end group 1739@end example 1740 1741@noindent 1742both @samp{1, 2, 3, 4} and @samp{4, 5, 6, 7} are comma separated lists. 1743 1744Comma separated lists cannot be directly manipulated by the 1745user. However, both structure arrays and cell arrays can be converted 1746into comma separated lists, and thus used in place of explicitly 1747written comma separated lists. This feature is useful in many ways, 1748as will be shown in the following subsections. 1749 1750@menu 1751* Comma Separated Lists Generated from Cell Arrays:: 1752* Comma Separated Lists Generated from Structure Arrays:: 1753@end menu 1754 1755@node Comma Separated Lists Generated from Cell Arrays 1756@subsection Comma Separated Lists Generated from Cell Arrays 1757 1758As has been mentioned above (@pxref{Indexing Cell Arrays}), elements 1759of a cell array can be extracted into a comma separated list with the 1760@code{@{} and @code{@}} operators. By surrounding this list with 1761@code{[} and @code{]}, it can be concatenated into an array. For example: 1762 1763@example 1764@group 1765a = @{1, [2, 3], 4, 5, 6@}; 1766b = [a@{1:4@}] 1767 @result{} b = 1768 1 2 3 4 5 1769@end group 1770@end example 1771 1772Similarly, it is possible to create a new cell array containing cell 1773elements selected with @code{@{@}}. By surrounding the list with 1774@samp{@{} and @samp{@}} a new cell array will be created, as the 1775following example illustrates: 1776 1777@example 1778@group 1779a = @{1, rand(2, 2), "three"@}; 1780b = @{ a@{ [1, 3] @} @} 1781 @result{} b = 1782 @{ 1783 [1,1] = 1 1784 [1,2] = three 1785 @} 1786@end group 1787@end example 1788 1789Furthermore, cell elements (accessed by @code{@{@}}) can be passed 1790directly to a function. The list of elements from the cell array will 1791be passed as an argument list to a given function as if it is called 1792with the elements as individual arguments. The two calls to 1793@code{printf} in the following example are identical but the latter is 1794simpler and can handle cell arrays of an arbitrary size: 1795 1796@example 1797@group 1798c = @{"GNU", "Octave", "is", "Free", "Software"@}; 1799printf ("%s ", c@{1@}, c@{2@}, c@{3@}, c@{4@}, c@{5@}); 1800 @print{} GNU Octave is Free Software 1801printf ("%s ", c@{:@}); 1802 @print{} GNU Octave is Free Software 1803@end group 1804@end example 1805 1806If used on the left-hand side of an assignment, a comma separated list 1807generated with @code{@{@}} can be assigned to. An example is 1808 1809@example 1810in@{1@} = [10, 20, 30]; 1811in@{2@} = inf; 1812in@{3@} = "last"; 1813in@{4@} = "first"; 1814out = cell (4, 1); 1815[out@{1:3@}] = in@{1 : 3@}; 1816[out@{4:6@}] = in@{[1, 2, 4]@}) 1817 @result{} out = 1818 @{ 1819 [1,1] = 1820 1821 10 20 30 1822 1823 [2,1] = Inf 1824 [3,1] = last 1825 [4,1] = 1826 1827 10 20 30 1828 1829 [5,1] = Inf 1830 [6,1] = first 1831 @} 1832@end example 1833 1834 1835@node Comma Separated Lists Generated from Structure Arrays 1836@subsection Comma Separated Lists Generated from Structure Arrays 1837Structure arrays can equally be used to create comma separated 1838lists. This is done by addressing one of the fields of a structure 1839array. For example: 1840 1841@example 1842@group 1843x = ceil (randn (10, 1)); 1844in = struct ("call1", @{x, 3, "last"@}, 1845 "call2", @{x, inf, "first"@}); 1846out = struct ("call1", cell (2, 1), "call2", cell (2, 1)); 1847[out.call1] = find (in.call1); 1848[out.call2] = find (in.call2); 1849@end group 1850@end example 1851