File: var.texi

package info (click to toggle)
octave 10.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 145,388 kB
  • sloc: cpp: 335,976; ansic: 82,241; fortran: 20,963; objc: 9,402; sh: 8,756; yacc: 4,392; lex: 4,333; perl: 1,544; java: 1,366; awk: 1,259; makefile: 659; xml: 192
file content (1098 lines) | stat: -rw-r--r-- 32,534 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
@c DO NOT EDIT!  Generated automatically by munge-texi.pl.

@c Copyright (C) 1996-2025 The Octave Project Developers
@c
@c This file is part of Octave.
@c
@c Octave is free software: you can redistribute it and/or modify it
@c under the terms of the GNU General Public License as published by
@c the Free Software Foundation, either version 3 of the License, or
@c (at your option) any later version.
@c
@c Octave is distributed in the hope that it will be useful, but
@c WITHOUT ANY WARRANTY; without even the implied warranty of
@c MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
@c GNU General Public License for more details.
@c
@c You should have received a copy of the GNU General Public License
@c along with Octave; see the file COPYING.  If not, see
@c <https://www.gnu.org/licenses/>.

@node Variables
@chapter Variables
@cindex variables, user-defined
@cindex user-defined variables

Variables let you give names to values and refer to them later.  You have
already seen variables in many of the examples.  The name of a variable
must be a sequence of letters, digits and underscores, but it may not begin
with a digit.  Octave does not enforce a limit on the length of variable
names, but it is seldom useful to have variables with names longer than
about 30 characters.  The following are all valid variable names

@example
@group
x
x15
__foo_bar_baz__
fucnrdthsucngtagdjb
@end group
@end example

@noindent
However, names like @code{__foo_bar_baz__} that begin and end with two
underscores are understood to be reserved for internal use by Octave.
You should not use them in code you write, except to access Octave's
documented internal variables and built-in symbolic constants.

Case is significant in variable names.  The symbols @code{a} and
@code{A} are distinct variables.

A variable name is a valid expression by itself.  It represents the
variable's current value.  Variables are given new values with
@dfn{assignment operators} and @dfn{increment operators}.
@xref{Assignment Ops,,Assignment Expressions}.

There is one automatically created variable with a special meaning.  The
@code{ans} variable always contains the result of the last computation,
where the output wasn't assigned to any variable.  The code @code{a =
cos (pi)} will assign the value -1 to the variable @code{a}, but will
not change the value of @code{ans}.  However, the code @code{cos (pi)}
will set the value of @code{ans} to -1.

Variables in Octave do not have fixed types, so it is possible to first
store a numeric value in a variable and then to later use the same name
to hold a string value in the same program.  Variables may not be used
before they have been given a value.  Doing so results in an error.

@cindex @code{ans}
@c ans scripts/help/ans.m
@anchor{XREFans}
@html
<span style="display:block; margin-top:-4.5ex;">&nbsp;</span>
@end html


@defvr {Automatic Variable} ans
The most recently computed result that was not explicitly assigned to a
variable.

For example, after the expression

@example
3^2 + 4^2
@end example

@noindent
is evaluated, the value returned by @code{ans} is 25.
@end defvr


@c isvarname libinterp/corefcn/utils.cc
@anchor{XREFisvarname}
@html
<span style="display:block; margin-top:-4.5ex;">&nbsp;</span>
@end html


@deftypefn {} {@var{tf} =} isvarname (@var{name})
Return true if @var{name} is a valid variable name.

A valid variable name is composed of letters, digits, and underscores ("_"),
and the first character must not be a digit.
@xseealso{@ref{XREFiskeyword,,iskeyword}, @ref{XREFexist,,exist}, @ref{XREFwho,,who}}
@end deftypefn


@c matlab.lang.makeValidName scripts/+matlab/+lang/makeValidName.m
@anchor{XREFmatlab_lang_makeValidName}
@html
<span style="display:block; margin-top:-4.5ex;">&nbsp;</span>
@end html


@deftypefn  {} {@var{varname} =} matlab.lang.makeValidName (@var{str})
@deftypefnx {} {@var{varname} =} matlab.lang.makeValidName (@dots{}, @qcode{"ReplacementStyle"}, @var{rs})
@deftypefnx {} {@var{varname} =} matlab.lang.makeValidName (@dots{}, @qcode{"Prefix"}, @var{pfx})
@deftypefnx {} {[@var{varname}, @var{ismodified}] =} matlab.lang.makeValidName (@dots{})

Create valid variable name @var{varname} from @var{str}.

The input @var{str} must be a string or a cell array of strings.
The output @var{varname} will be of the same type.

A valid variable name is a sequence of letters, digits, and underscores that
does not begin with a digit.

The @qcode{"ReplacementStyle"} option specifies how invalid characters
are handled.  Acceptable values are

@table @asis
@item @qcode{"underscore"} (default)
Replace all invalid characters with an underscore (@qcode{"_"}).

@item @qcode{"delete"}
Remove any invalid character.

@item @qcode{"hex"}
Replace all invalid characters with their hexadecimal representation.
@end table

Whitespace characters are always removed @strong{prior} to the application
of the @qcode{"ReplacementStyle"}.  Lowercase letters following a whitespace
will be changed to uppercase.

The @qcode{"Prefix"} option specifies the string @var{pfx} to add as a
prefix to the input if it begins with a digit.  @var{pfx} must be a valid
variable name itself.  The default prefix is @qcode{"x"}.

The optional output @var{ismodified} is a logical array indicating whether
the respective element in @var{str} was a valid name or not.

@xseealso{@ref{XREFiskeyword,,iskeyword}, @ref{XREFisvarname,,isvarname}, @ref{XREFmatlab_lang_makeUniqueStrings,,matlab.lang.makeUniqueStrings}}
@end deftypefn


@c matlab.lang.makeUniqueStrings scripts/+matlab/+lang/makeUniqueStrings.m
@anchor{XREFmatlab_lang_makeUniqueStrings}
@html
<span style="display:block; margin-top:-4.5ex;">&nbsp;</span>
@end html


@deftypefn  {} {@var{uniqstr} =} matlab.lang.makeUniqueStrings (@var{str})
@deftypefnx {} {@var{uniqstr} =} matlab.lang.makeUniqueStrings (@var{str}, @var{ex})
@deftypefnx {} {@var{uniqstr} =} matlab.lang.makeUniqueStrings (@var{str}, @var{ex}, @var{maxlength})
@deftypefnx {} {[@var{uniqstr}, @var{ismodified}] =} matlab.lang.makeUniqueStrings (@dots{})

Construct a list of unique strings from a list of strings.

The input @var{str} must be a string or a cell array of strings.
The output @var{uniqstr} will be of the same type.

The algorithm makes two strings unique by appending an underscore
(@qcode{"_"} and a numeric count to the second string.

If @var{ex} is a string or a cell array of strings, @var{uniqstr} will
contain elements that are unique between themselves and with respect to
@var{ex}.

If @var{ex} is an index array or a logical array for @var{str} then it
selects the subset of @var{str} that are made unique.  Unselected elements
are not modified.

The optional input @var{maxlength} specifies the maximum length of any
string in @var{uniqstr}.  If an input string cannot be made unique without
exceeding @var{maxlength} an error is emitted.

The optional output @var{ismodified} is a logical array indicating whether
each element in @var{str} was modified to make it unique.

@xseealso{@ref{XREFunique,,unique}, @ref{XREFmatlab_lang_makeValidName,,matlab.lang.makeValidName}}
@end deftypefn


@c namelengthmax scripts/miscellaneous/namelengthmax.m
@anchor{XREFnamelengthmax}
@html
<span style="display:block; margin-top:-4.5ex;">&nbsp;</span>
@end html


@deftypefn {} {@var{n} =} namelengthmax ()
Return the @sc{matlab} compatible maximum variable name length.

Octave is capable of storing strings up to @math{2^{31} - 1} in length.
However for @sc{matlab} compatibility all variable, function, and structure
field names should be shorter than the length returned by
@code{namelengthmax}.  In particular, variables stored to a @sc{matlab} file
format (@file{*.mat}) will have their names truncated to this length.
@end deftypefn


@menu
* Global Variables::
* Persistent Variables::
* Status of Variables::
@end menu

@node Global Variables
@section Global Variables
@cindex global variables
@cindex @code{global} statement
@cindex variables, global

See keyword:  @ref{XREFglobal, , global}

A @dfn{global} variable is one that may be accessed anywhere within Octave.
This is in contrast to a local variable which can only be accessed outside
of its current context if it is passed explicitly, such as by including it as a
parameter when calling a function
(@code{fcn (@var{local_var1}, @var{local_var2})}).

A variable is declared global by using a @code{global} declaration statement.
The following statements are all global declarations.

@example
@group
global a
global a b
global c = 2
global d = 3 e f = 5
@end group
@end example

Note that the @code{global} qualifier extends only to the next end-of-statement
indicator which could be a comma (@samp{,}), semicolon (@samp{;}), or newline
(@samp{'\n'}).  For example, the following code declares one global variable,
@var{a}, and one local variable @var{b} to which the value 1 is assigned.

@example
global a, b = 1
@end example

A global variable may only be initialized once in a @code{global} statement.
For example, after executing the following code

@example
@group
global gvar = 1
global gvar = 2
@end group
@end example

@noindent
the value of the global variable @code{gvar} is 1, not 2.  Issuing a
@samp{clear gvar} command does not change the above behavior, but
@samp{clear all} does.

It is necessary declare a variable as global within a function body in order to
access the one universal variable.  For example,

@example
@group
global x
function f ()
  x = 1;
endfunction
f ()
@end group
@end example

@noindent
does @emph{not} set the value of the global variable @code{x} to 1.  Instead,
a local variable, with name @code{x}, is created and assigned the value of 1.
In order to change the value of the global variable @code{x}, you must also
declare it to be global within the function body, like this

@example
@group
function f ()
  global x;
  x = 1;
endfunction
@end group
@end example

Passing a global variable in a function parameter list will make a local copy
and @emph{not} modify the global value.  For example, given the function

@example
@group
function f (x)
  x = 0
endfunction
@end group
@end example

@noindent
and the definition of @code{x} as a global variable at the top level,

@example
global x = 13
@end example

@noindent
the expression

@example
f (x)
@end example

@noindent
will display the value of @code{x} from inside the function as 0, but the value
of @code{x} at the top level remains unchanged, because the function works with
a @emph{copy} of its argument.

Programming Note: While global variables occasionally are the right solution to
a coding problem, modern best practice discourages their use.  Code which
relies on global variables may behave unpredictably between different users
and can be difficult to debug.  This is because global variables can introduce
systemic changes so that localizing a bug to a particular function, or to a
particular loop within a function, becomes difficult.

@c isglobal libinterp/corefcn/variables.cc
@anchor{XREFisglobal}
@html
<span style="display:block; margin-top:-4.5ex;">&nbsp;</span>
@end html


@deftypefn {} {@var{tf} =} isglobal (@var{name})
Return true if @var{name} is a globally visible variable.

For example:

@example
@group
global x
isglobal ("x")
   @result{} 1
@end group
@end example
@xseealso{@ref{XREFisvarname,,isvarname}, @ref{XREFexist,,exist}}
@end deftypefn


@node Persistent Variables
@section Persistent Variables
@cindex persistent variables
@cindex @code{persistent} statement
@cindex variables, persistent

See keyword:  @ref{XREFpersistent, , persistent}

A variable that has been declared @dfn{persistent} within a function
will retain its contents in memory between subsequent calls to the
same function.  The difference between persistent variables and global
variables is that persistent variables are local in scope to a
particular function and are not visible elsewhere.

The following example uses a persistent variable to create a function
that prints the number of times it has been called.

@example
@group
function count_calls ()
  persistent calls = 0;
  printf ("'count_calls' has been called %d times\n",
          ++calls);
endfunction

for i = 1:3
  count_calls ();
endfor

@print{} 'count_calls' has been called 1 times
@print{} 'count_calls' has been called 2 times
@print{} 'count_calls' has been called 3 times
@end group
@end example

As the example shows, a variable may be declared persistent using a
@code{persistent} declaration statement.  The following statements are
all persistent declarations.

@example
@group
persistent a
persistent a b
persistent c = 2
persistent d = 3 e f = 5
@end group
@end example

The behavior of persistent variables is equivalent to the behavior of
static variables in C@.

One restriction for persistent variables is, that neither input nor
output arguments of a function can be persistent:

@example
@group
function y = foo ()
  persistent y = 0;  # Not allowed!
endfunction

foo ()
@print{} error: can't make function parameter y persistent
@end group
@end example

Like global variables, a persistent variable may only be initialized once.
For example, after executing the following code

@example
@group
persistent pvar = 1
persistent pvar = 2
@end group
@end example

@noindent
the value of the persistent variable @code{pvar} is 1, not 2.

If a persistent variable is declared but not initialized to a specific
value, it will contain an empty matrix.  So, it is also possible to
initialize a persistent variable by checking whether it is empty, as the
following example illustrates.

@example
@group
function count_calls ()
  persistent calls;
  if (isempty (calls))
    calls = 0;
  endif
  printf ("'count_calls' has been called %d times\n",
          ++calls);
endfunction
@end group
@end example

@noindent
This implementation behaves in exactly the same way as the previous
implementation of @code{count_calls}.

The value of a persistent variable is kept in memory until it is
explicitly cleared.  Assuming that the implementation of @code{count_calls}
is saved on disk, we get the following behavior.

@example
for i = 1:2
  count_calls ();
endfor
@print{} 'count_calls' has been called 1 times
@print{} 'count_calls' has been called 2 times

clear
for i = 1:2
  count_calls ();
endfor
@print{} 'count_calls' has been called 3 times
@print{} 'count_calls' has been called 4 times

clear all
for i = 1:2
  count_calls ();
endfor
@print{} 'count_calls' has been called 1 times
@print{} 'count_calls' has been called 2 times

clear count_calls
for i = 1:2
  count_calls ();
endfor
@print{} 'count_calls' has been called 1 times
@print{} 'count_calls' has been called 2 times
@end example

@noindent
That is, the persistent variable is only removed from memory when the
function containing the variable is removed.  Note that if the function
definition is typed directly into the Octave prompt, the persistent
variable will be cleared by a simple @code{clear} command as the entire
function definition will be removed from memory.  If you do not want
a persistent variable to be removed from memory even if the function is
cleared, you should use the @code{mlock} function
(@pxref{Function Locking}).

@node Status of Variables
@section Status of Variables

When creating simple one-shot programs it can be very convenient to
see which variables are available at the prompt.  The function @code{who}
and its siblings @code{whos} and @code{whos_line_format} will show
different information about what is in memory, as the following shows.

@example
@group
str = "A random string";
who
 @print{} Variables in the current scope:
 @print{}
 @print{} ans  str
@end group
@end example

@c who libinterp/corefcn/call-stack.cc
@anchor{XREFwho}
@html
<span style="display:block; margin-top:-4.5ex;">&nbsp;</span>
@end html


@deftypefn  {} {} who
@deftypefnx {} {} who pattern @dots{}
@deftypefnx {} {} who option pattern @dots{}
@deftypefnx {} {C =} who (@dots{})
List currently defined variables matching the given patterns.

Valid pattern syntax is the same as described for the @code{clear} command.
If no patterns are supplied, all variables are listed.

By default, only variables visible in the local scope are displayed.

The following are valid options, but may not be combined.

@table @code
@item global
List variables in the global scope rather than the current scope.

@item -regexp
The patterns are considered to be regular expressions when matching the
variables to display.  The same pattern syntax accepted by the @code{regexp}
function is used.

@item -file
The next argument is treated as a filename.  All variables found within the
specified file are listed.  No patterns are accepted when reading variables
from a file.
@end table

If called as a function, return a cell array of defined variable names
matching the given patterns.
@xseealso{@ref{XREFwhos,,whos}, @ref{XREFisglobal,,isglobal}, @ref{XREFisvarname,,isvarname}, @ref{XREFexist,,exist}, @ref{XREFregexp,,regexp}}
@end deftypefn


@c whos libinterp/corefcn/call-stack.cc
@anchor{XREFwhos}
@html
<span style="display:block; margin-top:-4.5ex;">&nbsp;</span>
@end html


@deftypefn  {} {} whos
@deftypefnx {} {} whos pattern @dots{}
@deftypefnx {} {} whos option pattern @dots{}
@deftypefnx {} {S =} whos ("pattern", @dots{})
Provide detailed information on currently defined variables matching the
given patterns.

Options and pattern syntax are the same as for the @code{who} command.

Extended information about each variable is summarized in a table with the
following default entries.

@table @asis
@item Attr
Attributes of the listed variable.  Possible attributes are:

@table @asis
@item blank
Variable in local scope

@item @code{c}
Variable of complex type.

@item @code{f}
Formal parameter (function argument).

@item @code{g}
Variable with global scope.

@item @code{p}
Persistent variable.
@end table

@item Name
The name of the variable.

@item Size
The logical size of the variable.  A scalar is 1x1, a vector is
@nospell{1xN} or @nospell{Nx1}, a 2-D matrix is @nospell{MxN}.

@item Bytes
The amount of memory currently used to store the variable.

@item Class
The class of the variable.  Examples include double, single, char, uint16,
cell, and struct.
@end table

The table can be customized to display more or less information through
the function @code{whos_line_format}.

If @code{whos} is called as a function, return a struct array of defined
variable names matching the given patterns.  Fields in the structure
describing each variable are: name, size, bytes, class, global, sparse,
complex, nesting, persistent.
@xseealso{@ref{XREFwho,,who}, @ref{XREFwhos_line_format,,whos_line_format}}
@end deftypefn


@c whos_line_format libinterp/parse-tree/pt-eval.cc
@anchor{XREFwhos_line_format}
@html
<span style="display:block; margin-top:-4.5ex;">&nbsp;</span>
@end html


@deftypefn  {} {@var{val} =} whos_line_format ()
@deftypefnx {} {@var{old_val} =} whos_line_format (@var{new_val})
@deftypefnx {} {@var{old_val} =} whos_line_format (@var{new_val}, "local")
Query or set the format string used by the command @code{whos}.

A full format string is:
@c Set example in small font to prevent overfull line

@smallexample
%[modifier]<command>[:width[:left-min[:balance]]];
@end smallexample

The following command sequences are available:

@table @code
@item %a
Prints attributes of variables (c=complex, s=sparse, f=formal parameter,
g=global, p=persistent).

@item %b
Prints number of bytes occupied by variables.

@item %c
Prints class names of variables.

@item %e
Prints elements held by variables.

@item %n
Prints variable names.

@item %s
Prints dimensions of variables.

@item %t
Prints type names of variables.
@end table

Every command may also have an alignment modifier:

@table @code
@item l
Left alignment.

@item r
Right alignment (default).

@item c
Column-aligned (only applicable to command %s).
@end table

The @code{width} parameter is a positive integer specifying the minimum
number of columns used for printing.  No maximum is needed as the field will
auto-expand as required.

The parameters @code{left-min} and @code{balance} are only available when
the column-aligned modifier is used with the command @samp{%s}.
@code{balance} specifies the column number within the field width which
will be aligned between entries.  Numbering starts from 0 which indicates
the leftmost column.  @code{left-min} specifies the minimum field width to
the left of the specified balance column.

The default format is:

@example
"  %la:5; %ln:6; %cs:16:6:1;  %rb:12;  %lc:-1;@backslashchar{}n"
@end example

When called from inside a function with the @qcode{"local"} option, the
variable is changed locally for the function and any subroutines it calls.
The original variable value is restored when exiting the function.
@xseealso{@ref{XREFwhos,,whos}}
@end deftypefn


Instead of displaying which variables are in memory, it is possible
to determine if a given variable is available.  That way it is possible
to alter the behavior of a program depending on the existence of a
variable.  The following example illustrates this.

@example
@group
if (! exist ("meaning", "var"))
  disp ("The program has no 'meaning'");
endif
@end group
@end example

@c exist libinterp/corefcn/variables.cc
@anchor{XREFexist}
@html
<span style="display:block; margin-top:-4.5ex;">&nbsp;</span>
@end html


@deftypefn  {} {@var{c} =} exist (@var{name})
@deftypefnx {} {@var{c} =} exist (@var{name}, @var{type})
Check for the existence of @var{name} as a variable, function, file, directory,
or class.

The return code @var{c} is one of

@table @asis
@item 1
@var{name} is a variable.

@item 2
@var{name} is an absolute filename, an ordinary file in Octave's @code{path},
or (after appending @samp{.m}) a function file in Octave's @code{path}.

@item 3
@var{name} is a @samp{.oct} or @samp{.mex} file in Octave's @code{path}.

@item 5
@var{name} is a built-in function.

@item 7
@var{name} is a directory.

@item 8
@var{name} is a classdef class.

@item 103
@var{name} is a function not associated with a file (entered on the command
line).

@item 0
@var{name} does not exist.
@end table

If the optional argument @var{type} is supplied, check only for symbols of the
specified type.  Valid types are

@table @asis
@item @qcode{"var"}
Check only for variables.

@item @qcode{"builtin"}
Check only for built-in functions.

@item @qcode{"dir"}
Check only for directories.

@item @qcode{"file"}
Check only for files and directories.

@item @qcode{"class"}
Check only for classdef classes.
@end table

If no type is given, and there are multiple possible matches for name,
@code{exist} will return a code according to the following priority list:
variable, built-in function, oct-file, directory, file, class.

@code{exist} returns 2 if a regular file called @var{name} is present in
Octave's search path.  For information about other types of files not on the
search path use some combination of the functions @code{file_in_path} and
@code{stat} instead.

Programming Note: If @var{name} is implemented by a buggy .oct/.mex file,
calling @var{exist} may cause Octave to crash.  To maintain high performance,
Octave trusts .oct/.mex files instead of @nospell{sandboxing} them.

@xseealso{@ref{XREFfile_in_loadpath,,file_in_loadpath}, @ref{XREFfile_in_path,,file_in_path}, @ref{XREFdir_in_loadpath,,dir_in_loadpath}, @ref{XREFstat,,stat}}
@end deftypefn


Usually Octave will manage the memory, but sometimes it can be practical
to remove variables from memory manually.  This is usually needed when
working with large variables that fill a substantial part of the memory.
On a computer that uses the IEEE@tie{}754 floating point format, the following
program allocates a matrix that requires around 128 MB memory.

@example
large_matrix = zeros (4000, 4000);
@end example

@noindent
Since having this variable in memory might slow down other computations,
it can be necessary to remove it manually from memory.  The @code{clear}
or @code{clearvars} functions do this.

@c clear libinterp/corefcn/variables.cc
@anchor{XREFclear}
@html
<span style="display:block; margin-top:-4.5ex;">&nbsp;</span>
@end html


@deftypefn  {} {} clear
@deftypefnx {} {} clear @var{pattern} @dots{}
@deftypefnx {} {} clear @var{options} @var{pattern} @dots{}
Delete the names matching the given @var{pattern}s thereby freeing memory.

The @var{pattern} may contain the following special characters:

@table @code
@item ?
Match any single character.

@item *
Match zero or more characters.

@item [ @var{list} ]
Match the list of characters specified by @var{list}.  If the first character
is @code{!} or @code{^}, match all characters except those specified by
@var{list}.  For example, the pattern @code{[a-zA-Z]} will match all lowercase
and uppercase alphabetic characters.  On Windows, square brackets are matched
literally and are not used to group characters.
@end table

For example, the command

@example
clear foo b*r
@end example

@noindent
clears the name @code{foo} and all names that begin with the letter @samp{b}
and end with the letter @samp{r}.

If @code{clear} is called without any arguments, all user-defined variables
are cleared from the current workspace (i.e., local variables).  Any global
variables present will no longer be visible in the current workspace, but they
will continue to exist in the global workspace.  Functions are unaffected by
this form of @code{clear}.

The following options are available in both long and short form

@table @code
@item all, -all, -a
Clear all local and global user-defined variables, and all functions from the
symbol table.

@item -exclusive, -x
Clear variables that do @strong{not} match the following pattern.

@item functions, -functions, -f
Clear function names from the function symbol table.  Persistent variables
will be re-initialized to their default value unless the function has been
locked in memory with @code{mlock}.

@item global, -global, -g
Clear global variable names.

@item variables, -variables, -v
Clear local variable names.

@item classes, -classes, -c
Clear the class structure table and all objects.

@item -regexp, -r
The @var{pattern} arguments are treated as regular expressions and any matches
will be cleared.
@end table

With the exception of @option{-exclusive} and @option{-regexp}, all long
options can be used without the dash as well.  Note that, aside from
@option{-exclusive}, only one other option may appear.  All options must
appear before any patterns.

Programming Notes: The command @code{clear @var{name}} only clears the variable
@var{name} when both a variable and a (shadowed) function named @var{name}
are currently defined.  For example, suppose you have defined a function
@code{foo}, and then hidden it by performing the assignment @code{foo = 2}.
Executing the command @code{clear foo} once will clear the variable
definition and restore the definition of @code{foo} as a function.
Executing @code{clear foo} a second time will clear the function definition.

When a local variable name, which is linked to a global variable, is cleared
only the local copy of the variable is removed.  The global copy is untouched
and can be restored with @code{global @var{global_varname}}.  Conversely,
@code{clear -g @var{global_varname}} will remove both the local and global
variables.

@xseealso{@ref{XREFclearvars,,clearvars}, @ref{XREFwho,,who}, @ref{XREFwhos,,whos}, @ref{XREFexist,,exist}, @ref{XREFmlock,,mlock}}
@end deftypefn


@c clearvars scripts/miscellaneous/clearvars.m
@anchor{XREFclearvars}
@html
<span style="display:block; margin-top:-4.5ex;">&nbsp;</span>
@end html


@deftypefn  {} {} clearvars
@deftypefnx {} {} clearvars @var{pattern} @dots{}
@deftypefnx {} {} clearvars -regexp @var{pattern} @dots{}
@deftypefnx {} {} clearvars @dots{} -except @var{pattern} @dots{}
@deftypefnx {} {} clearvars @dots{} -except -regexp @var{pattern} @dots{}
@deftypefnx {} {} clearvars -global @dots{}
Delete the variables matching the given @var{pattern}s from memory.

The @var{pattern} may contain the following special characters:

@table @code
@item ?
Match any single character.

@item *
Match zero or more characters.

@item [ @var{list} ]
Match the list of characters specified by @var{list}.  If the first
character is @code{!} or @code{^}, match all characters except those
specified by @var{list}.  For example, the pattern @code{[a-zA-Z]} will
match all lowercase and uppercase alphabetic characters.
@end table

If the @option{-regexp} option is given then subsequent patterns are treated
as regular expressions and any matches will be cleared.

If the @option{-except} option is given then subsequent patterns select
variables that will @strong{not} be cleared.

If the @option{-global} option is given then all patterns will be applied
to global variables rather than local variables.

When called with no arguments, @code{clearvars} deletes all local variables.

Example Code:

Clear all variables starting with @qcode{'x'} and the specific variable
@qcode{"foobar"}

@example
clearvars x* foobar
@end example

Clear the specific variable @qcode{"foobar"} and use regular expressions to
clear all variables starting with @qcode{'x'} or @qcode{'y'}.

@example
clearvars foobar -regexp ^x ^y
@end example

Clear all variables except for @qcode{"foobar"}

@example
clearvars -except foobar
@end example

Clear all variables beginning with @qcode{"foo"}, except for those ending
in @qcode{"bar"}

@example
clearvars foo* -except -regexp bar$
@end example

@xseealso{@ref{XREFclear,,clear}, @ref{XREFwho,,who}, @ref{XREFwhos,,whos}, @ref{XREFexist,,exist}}
@end deftypefn


@c pack scripts/miscellaneous/pack.m
@anchor{XREFpack}
@html
<span style="display:block; margin-top:-4.5ex;">&nbsp;</span>
@end html


@deftypefn {} {} pack ()
Consolidate workspace memory in @sc{matlab}.

This function is provided for compatibility, but does nothing in Octave.
@xseealso{@ref{XREFclear,,clear}}
@end deftypefn


Information about a function or variable such as its location in the
file system can also be acquired from within Octave.  This is usually
only useful during development of programs, and not within a program.

@c type scripts/help/type.m
@anchor{XREFtype}
@html
<span style="display:block; margin-top:-4.5ex;">&nbsp;</span>
@end html


@deftypefn  {} {} type @var{name} @dots{}
@deftypefnx {} {} type -q @var{name} @dots{}
@deftypefnx {} {text =} type ("@var{name}", @dots{})
Display the contents of @var{name} which may be a file, function (m-file),
variable, operator, or keyword.

@code{type} normally prepends a header line describing the category of
@var{name} such as function or variable; The @option{-q} option suppresses
this behavior.

If no output variable is used the contents are displayed on screen.
Otherwise, a cell array of strings is returned, where each element
corresponds to the contents of each requested function.
@end deftypefn


@c which scripts/help/which.m
@anchor{XREFwhich}
@html
<span style="display:block; margin-top:-4.5ex;">&nbsp;</span>
@end html


@deftypefn  {} {} which @var{name} @dots{}
@deftypefnx {} {[@var{str}, @dots{}] =} which ('@var{name}', @dots{})
Display the type of each @var{name}.

If @var{name} is defined from a function file, the full name of the file is
also displayed.
@xseealso{@ref{XREFhelp,,help}, @ref{XREFlookfor,,lookfor}}
@end deftypefn


@c what scripts/miscellaneous/what.m
@anchor{XREFwhat}
@html
<span style="display:block; margin-top:-4.5ex;">&nbsp;</span>
@end html


@deftypefn  {} {} what
@deftypefnx {} {} what @var{dir}
@deftypefnx {} {w =} what (@var{dir})
List the Octave specific files in directory @var{dir}.

If @var{dir} is not specified then the current directory is used.

If a return argument is requested, the files found are returned in the
structure @var{w}.  The structure contains the following fields:

@table @asis
@item path
Full path to directory @var{dir}

@item m
Cell array of m-files

@item mat
Cell array of mat files

@item mex
Cell array of mex files

@item oct
Cell array of oct files

@item mdl
Cell array of mdl files

@item slx
Cell array of slx files

@item p
Cell array of p-files

@item classes
Cell array of class directories (@file{@@@var{classname}/})

@item packages
Cell array of package directories (@file{+@var{pkgname}/})
@end table

Compatibility Note: Octave does not support mdl, slx, and p files.
@code{what} will always return an empty list for these categories.
@xseealso{@ref{XREFwhich,,which}, @ref{XREFls,,ls}, @ref{XREFexist,,exist}}
@end deftypefn