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
|
<!DOCTYPE html>
<html>
<!-- Created by GNU Texinfo 7.1.1, https://www.gnu.org/software/texinfo/ -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Numerical Data and Strings (GNU Octave (version 10.3.0))</title>
<meta name="description" content="Numerical Data and Strings (GNU Octave (version 10.3.0))">
<meta name="keywords" content="Numerical Data and Strings (GNU Octave (version 10.3.0))">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta name="viewport" content="width=device-width,initial-scale=1">
<link href="index.html" rel="start" title="Top">
<link href="Concept-Index.html" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Converting-Strings.html" rel="up" title="Converting Strings">
<link href="JSON-data-encoding_002fdecoding.html" rel="next" title="JSON data encoding/decoding">
<link href="String-encoding.html" rel="prev" title="String encoding">
<style type="text/css">
<!--
a.copiable-link {visibility: hidden; text-decoration: none; line-height: 0em}
div.example {margin-left: 3.2em}
span:hover a.copiable-link {visibility: visible}
strong.def-name {font-family: monospace; font-weight: bold; font-size: larger}
ul.mark-bullet {list-style-type: disc}
-->
</style>
<link rel="stylesheet" type="text/css" href="octave.css">
</head>
<body lang="en">
<div class="subsection-level-extent" id="Numerical-Data-and-Strings">
<div class="nav-panel">
<p>
Next: <a href="JSON-data-encoding_002fdecoding.html" accesskey="n" rel="next">JSON data encoding/decoding</a>, Previous: <a href="String-encoding.html" accesskey="p" rel="prev">String encoding</a>, Up: <a href="Converting-Strings.html" accesskey="u" rel="up">Converting Strings</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<h4 class="subsection" id="Numerical-Data-and-Strings-1"><span>5.4.2 Numerical Data and Strings<a class="copiable-link" href="#Numerical-Data-and-Strings-1"> ¶</a></span></h4>
<p>Apart from the string concatenation functions (see <a class="pxref" href="Concatenating-Strings.html">Concatenating Strings</a>)
which cast numerical data to the corresponding UTF-8 encoded characters, there
are several functions that format numerical data as strings. <code class="code">mat2str</code>
and <code class="code">num2str</code> convert real or complex matrices, while <code class="code">int2str</code>
converts integer matrices. <code class="code">int2str</code> takes the real part of complex
values and round fractional values to integer. A more flexible way to format
numerical data as strings is the <code class="code">sprintf</code> function
(see <a class="pxref" href="Formatted-Output.html">Formatted Output</a>, <a class="ref" href="Formatted-Output.html#XREFsprintf">sprintf</a>).
</p>
<a class="anchor" id="XREFmat2str"></a><span style="display:block; margin-top:-4.5ex;"> </span>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-mat2str"><span><code class="def-type"><var class="var">s</var> =</code> <strong class="def-name">mat2str</strong> <code class="def-code-arguments">(<var class="var">x</var>, <var class="var">n</var>)</code><a class="copiable-link" href="#index-mat2str"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-mat2str-1"><span><code class="def-type"><var class="var">s</var> =</code> <strong class="def-name">mat2str</strong> <code class="def-code-arguments">(<var class="var">x</var>, <var class="var">n</var>, "class")</code><a class="copiable-link" href="#index-mat2str-1"> ¶</a></span></dt>
<dd><p>Format real, complex, and logical matrices as strings.
</p>
<p>The returned string may be used to reconstruct the original matrix by using
the <code class="code">eval</code> function.
</p>
<p>The precision of the values is given by <var class="var">n</var>. If <var class="var">n</var> is a scalar
then both real and imaginary parts of the matrix are printed to the same
precision. Otherwise <code class="code"><var class="var">n</var>(1)</code> defines the precision of the real
part and <code class="code"><var class="var">n</var>(2)</code> defines the precision of the imaginary part.
The default for <var class="var">n</var> is 15.
</p>
<p>If the argument <code class="code">"class"</code> is given then the class of <var class="var">x</var> is
included in the string in such a way that <code class="code">eval</code> will result in the
construction of a matrix of the same class.
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">mat2str ([ -1/3 + i/7; 1/3 - i/7 ], [4 2])
⇒ "[-0.3333+0.14i;0.3333-0.14i]"
mat2str ([ -1/3 +i/7; 1/3 -i/7 ], [4 2])
⇒ "[-0.3333+0i 0+0.14i;0.3333+0i -0-0.14i]"
mat2str (int16 ([1 -1]), "class")
⇒ "int16([1 -1])"
mat2str (logical (eye (2)))
⇒ "[true false;false true]"
isequal (x, eval (mat2str (x)))
⇒ 1
</pre></div></div>
<p><strong class="strong">See also:</strong> <a class="ref" href="Formatted-Output.html#XREFsprintf">sprintf</a>, <a class="ref" href="#XREFnum2str">num2str</a>, <a class="ref" href="#XREFint2str">int2str</a>.
</p></dd></dl>
<a class="anchor" id="XREFnum2str"></a><span style="display:block; margin-top:-4.5ex;"> </span>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-num2str"><span><code class="def-type"><var class="var">str</var> =</code> <strong class="def-name">num2str</strong> <code class="def-code-arguments">(<var class="var">x</var>)</code><a class="copiable-link" href="#index-num2str"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-num2str-1"><span><code class="def-type"><var class="var">str</var> =</code> <strong class="def-name">num2str</strong> <code class="def-code-arguments">(<var class="var">x</var>, <var class="var">precision</var>)</code><a class="copiable-link" href="#index-num2str-1"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-num2str-2"><span><code class="def-type"><var class="var">str</var> =</code> <strong class="def-name">num2str</strong> <code class="def-code-arguments">(<var class="var">x</var>, <var class="var">format</var>)</code><a class="copiable-link" href="#index-num2str-2"> ¶</a></span></dt>
<dd><p>Convert a number (or array) to a string (or a character array).
</p>
<p>The optional second argument may either give the number of significant
digits (<var class="var">precision</var>) to be used in the output or a format template
string (<var class="var">format</var>) as in <code class="code">sprintf</code> (see <a class="pxref" href="Formatted-Output.html">Formatted Output</a>).
<code class="code">num2str</code> can also process complex numbers.
</p>
<p>Examples:
</p>
<div class="example">
<pre class="example-preformatted">num2str (123.456)
⇒ 123.456
num2str (123.456, 4)
⇒ 123.5
s = num2str ([1, 1.34; 3, 3.56], "%5.1f")
⇒ s =
1.0 1.3
3.0 3.6
whos s
⇒ Variables in the current scope:
Attr Name Size Bytes Class
==== ==== ==== ===== =====
s 2x8 16 char
Total is 16 elements using 16 bytes
num2str (1.234 + 27.3i)
⇒ 1.234+27.3i
</pre></div>
<p>The <code class="code">num2str</code> function is not very flexible. For better control
over the results, use <code class="code">sprintf</code> (see <a class="pxref" href="Formatted-Output.html">Formatted Output</a>).
</p>
<p>Programming Notes:
</p>
<p>For <small class="sc">MATLAB</small> compatibility, leading spaces are stripped before returning
the string.
</p>
<p>Integers larger than <code class="code">flintmax</code> may not be displayed correctly.
</p>
<p>For complex <var class="var">x</var>, the format string may only contain one output
conversion specification and nothing else. Otherwise, results will be
unpredictable.
</p>
<p>Any optional <var class="var">format</var> specified by the programmer is used without
modification. This is in contrast to <small class="sc">MATLAB</small> which tampers with the
<var class="var">format</var> based on internal heuristics.
</p>
<p><strong class="strong">See also:</strong> <a class="ref" href="Formatted-Output.html#XREFsprintf">sprintf</a>, <a class="ref" href="#XREFint2str">int2str</a>, <a class="ref" href="#XREFmat2str">mat2str</a>.
</p></dd></dl>
<a class="anchor" id="XREFint2str"></a><span style="display:block; margin-top:-4.5ex;"> </span>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-int2str"><span><code class="def-type"><var class="var">str</var> =</code> <strong class="def-name">int2str</strong> <code class="def-code-arguments">(<var class="var">n</var>)</code><a class="copiable-link" href="#index-int2str"> ¶</a></span></dt>
<dd><p>Convert an integer (or array of integers) to a string (or a character
array).
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">int2str (123)
⇒ 123
s = int2str ([1, 2, 3; 4, 5, 6])
⇒ s =
1 2 3
4 5 6
whos s
⇒ Variables in the current scope:
Attr Name Size Bytes Class
==== ==== ==== ===== =====
s 2x7 14 char
Total is 14 elements using 14 bytes
</pre></div></div>
<p>This function is not very flexible. For better control over the
results, use <code class="code">sprintf</code> (see <a class="pxref" href="Formatted-Output.html">Formatted Output</a>).
</p>
<p>Programming Notes:
</p>
<p>Non-integers are rounded to integers before display. Only the real part
of complex numbers is displayed.
</p>
<p><strong class="strong">See also:</strong> <a class="ref" href="Formatted-Output.html#XREFsprintf">sprintf</a>, <a class="ref" href="#XREFnum2str">num2str</a>, <a class="ref" href="#XREFmat2str">mat2str</a>.
</p></dd></dl>
<a class="anchor" id="XREFstr2double"></a><span style="display:block; margin-top:-4.5ex;"> </span>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-str2double"><span><code class="def-type"><var class="var">d</var> =</code> <strong class="def-name">str2double</strong> <code class="def-code-arguments">(<var class="var">str</var>)</code><a class="copiable-link" href="#index-str2double"> ¶</a></span></dt>
<dd><p>Convert a string to a real or complex number.
</p>
<p>The string must be in one of the following formats where a and b are real
numbers and the complex unit is <code class="code">'i'</code> or <code class="code">'j'</code>:
</p>
<ul class="itemize mark-bullet">
<li>a + bi
</li><li>a + b*i
</li><li>a + i*b
</li><li>bi + a
</li><li>b*i + a
</li><li>i*b + a
</li></ul>
<p>If present, a and/or b are of the form [+-]d[,.]d[[eE][+-]d] where
the brackets indicate optional arguments and <code class="code">'d'</code> indicates zero or
more digits. The special input values <code class="code">Inf</code>, <code class="code">NaN</code>, and <code class="code">NA</code>
are also accepted.
</p>
<p><var class="var">str</var> may be a character string, character matrix, or cell array. For
character arrays the conversion is repeated for every row, and a double or
complex array is returned. Empty rows in <var class="var">s</var> are deleted and not
returned in the numeric array. For cell arrays each character string
element is processed and a double or complex array of the same dimensions as
<var class="var">str</var> is returned.
</p>
<p>For unconvertible scalar or character string input <code class="code">str2double</code> returns
a NaN. Similarly, for character array input <code class="code">str2double</code> returns a
NaN for any row of <var class="var">s</var> that could not be converted. For a cell array,
<code class="code">str2double</code> returns a NaN for any element of <var class="var">s</var> for which
conversion fails. Note that numeric elements in a mixed string/numeric
cell array are not strings and the conversion will fail for these elements
and return NaN.
</p>
<p>Programming Note: <code class="code">str2double</code> can replace <code class="code">str2num</code>, is more
efficient, and avoids the security risk of using <code class="code">eval</code> on unknown data.
</p>
<p><strong class="strong">See also:</strong> <a class="ref" href="#XREFstr2num">str2num</a>.
</p></dd></dl>
<a class="anchor" id="XREFstr2num"></a><span style="display:block; margin-top:-4.5ex;"> </span>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-str2num"><span><code class="def-type"><var class="var">x</var> =</code> <strong class="def-name">str2num</strong> <code class="def-code-arguments">(<var class="var">s</var>)</code><a class="copiable-link" href="#index-str2num"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-str2num-1"><span><code class="def-type">[<var class="var">x</var>, <var class="var">state</var>] =</code> <strong class="def-name">str2num</strong> <code class="def-code-arguments">(<var class="var">s</var>)</code><a class="copiable-link" href="#index-str2num-1"> ¶</a></span></dt>
<dd><p>Convert the string (or character array) <var class="var">s</var> to a number (or an array).
</p>
<p>Examples:
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">str2num ("3.141596")
⇒ 3.141596
str2num (["1, 2, 3"; "4, 5, 6"])
⇒ 1 2 3
4 5 6
</pre></div></div>
<p>The optional second output, <var class="var">state</var>, is logically true when the
conversion is successful. If the conversion fails the numeric output,
<var class="var">x</var>, is empty and <var class="var">state</var> is false.
</p>
<p><strong class="strong">Caution:</strong> As <code class="code">str2num</code> uses the <code class="code">eval</code> function to do the
conversion, <code class="code">str2num</code> will execute any code contained in the string
<var class="var">s</var>. Use <code class="code">str2double</code> for a safer and faster conversion.
</p>
<p>For cell array of strings use <code class="code">str2double</code>.
</p>
<p><strong class="strong">See also:</strong> <a class="ref" href="#XREFstr2double">str2double</a>, <a class="ref" href="Evaluation.html#XREFeval">eval</a>.
</p></dd></dl>
<a class="anchor" id="XREFbin2dec"></a><span style="display:block; margin-top:-4.5ex;"> </span>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-bin2dec"><span><code class="def-type"><var class="var">d</var> =</code> <strong class="def-name">bin2dec</strong> <code class="def-code-arguments">(<var class="var">str</var>)</code><a class="copiable-link" href="#index-bin2dec"> ¶</a></span></dt>
<dd><p>Return the decimal number corresponding to the binary number represented
by the string <var class="var">str</var>.
</p>
<p>For example:
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">bin2dec ("1110")
⇒ 14
</pre></div></div>
<p>Spaces are ignored during conversion and may be used to make the binary
number more readable.
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">bin2dec ("1000 0001")
⇒ 129
</pre></div></div>
<p>If <var class="var">str</var> is a string matrix, return a column vector with one converted
number per row of <var class="var">str</var>; Invalid rows evaluate to NaN.
</p>
<p>If <var class="var">str</var> is a cell array of strings, return a column vector with one
converted number per cell element in <var class="var">str</var>.
</p>
<p><strong class="strong">See also:</strong> <a class="ref" href="#XREFdec2bin">dec2bin</a>, <a class="ref" href="#XREFbase2dec">base2dec</a>, <a class="ref" href="#XREFhex2dec">hex2dec</a>.
</p></dd></dl>
<a class="anchor" id="XREFdec2bin"></a><span style="display:block; margin-top:-4.5ex;"> </span>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-dec2bin"><span><code class="def-type"><var class="var">bstr</var> =</code> <strong class="def-name">dec2bin</strong> <code class="def-code-arguments">(<var class="var">d</var>)</code><a class="copiable-link" href="#index-dec2bin"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-dec2bin-1"><span><code class="def-type"><var class="var">bstr</var> =</code> <strong class="def-name">dec2bin</strong> <code class="def-code-arguments">(<var class="var">d</var>, <var class="var">len</var>)</code><a class="copiable-link" href="#index-dec2bin-1"> ¶</a></span></dt>
<dd><p>Return a string of ones and zeros representing the conversion of the integer
<var class="var">d</var> to a binary number.
</p>
<p>If <var class="var">d</var> is a matrix or cell array, return a string matrix with one row
for each element in <var class="var">d</var>, padded with leading zeros to the width of the
largest value.
</p>
<p>The optional second argument, <var class="var">len</var>, specifies the minimum number of
digits in the result.
</p>
<p>For negative elements of <var class="var">d</var>, return the binary value of the two’s
complement. The result is padded with leading ones to 8, 16, 32, or 64
bits as appropriate for the magnitude of the input. Positive input
elements are padded with leading zeros to the same width.
</p>
<p>Examples:
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">dec2bin (14)
⇒ "1110"
dec2bin (-14)
⇒ "11110010"
</pre></div></div>
<p>Programming tip: <code class="code">dec2bin</code> discards any fractional part of the input.
If you need the fractional part to be converted too, call <code class="code">dec2base</code>
with a nonzero number of decimal places. You can also use <code class="code">fix</code> or
<code class="code">round</code> on fractional inputs to ensure predictable rounding behavior.
</p>
<p><strong class="strong">See also:</strong> <a class="ref" href="#XREFbin2dec">bin2dec</a>, <a class="ref" href="#XREFdec2base">dec2base</a>, <a class="ref" href="#XREFdec2hex">dec2hex</a>.
</p></dd></dl>
<a class="anchor" id="XREFdec2hex"></a><span style="display:block; margin-top:-4.5ex;"> </span>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-dec2hex"><span><code class="def-type"><var class="var">hstr</var> =</code> <strong class="def-name">dec2hex</strong> <code class="def-code-arguments">(<var class="var">d</var>)</code><a class="copiable-link" href="#index-dec2hex"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-dec2hex-1"><span><code class="def-type"><var class="var">hstr</var> =</code> <strong class="def-name">dec2hex</strong> <code class="def-code-arguments">(<var class="var">d</var>, <var class="var">len</var>)</code><a class="copiable-link" href="#index-dec2hex-1"> ¶</a></span></dt>
<dd><p>Return a string representing the conversion of the integer <var class="var">d</var> to a
hexadecimal (base16) number.
</p>
<p>If <var class="var">d</var> is negative, return the hexadecimal complement of <var class="var">d</var>.
</p>
<p>If <var class="var">d</var> is a matrix or cell array, return a string matrix with one row
for each element in <var class="var">d</var>, padded with leading zeros to the width of the
largest value.
</p>
<p>The optional second argument, <var class="var">len</var>, specifies the minimum number of
digits in the result.
</p>
<p>Examples:
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">dec2hex (2748)
⇒ "ABC"
dec2hex (-2)
⇒ "FE"
</pre></div></div>
<p>Programming tip: <code class="code">dec2hex</code> discards any fractional part of the input.
If you need the fractional part to be converted too, call <code class="code">dec2base</code>
with a nonzero number of decimal places. You can also use <code class="code">fix</code> or
<code class="code">round</code> on fractional inputs to ensure predictable rounding behavior.
</p>
<p><strong class="strong">See also:</strong> <a class="ref" href="#XREFhex2dec">hex2dec</a>, <a class="ref" href="#XREFdec2base">dec2base</a>, <a class="ref" href="#XREFdec2bin">dec2bin</a>.
</p></dd></dl>
<a class="anchor" id="XREFhex2dec"></a><span style="display:block; margin-top:-4.5ex;"> </span>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-hex2dec"><span><code class="def-type"><var class="var">d</var> =</code> <strong class="def-name">hex2dec</strong> <code class="def-code-arguments">(<var class="var">str</var>)</code><a class="copiable-link" href="#index-hex2dec"> ¶</a></span></dt>
<dd><p>Return the integer corresponding to the hexadecimal number represented by
the string <var class="var">str</var>.
</p>
<p>For example:
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">hex2dec ("12B")
⇒ 299
hex2dec ("12b")
⇒ 299
</pre></div></div>
<p>If <var class="var">str</var> is a string matrix, return a column vector with one converted
number per row of <var class="var">str</var>; Invalid rows evaluate to NaN.
</p>
<p>If <var class="var">str</var> is a cell array of strings, return a column vector with one
converted number per cell element in <var class="var">str</var>.
</p>
<p><strong class="strong">See also:</strong> <a class="ref" href="#XREFdec2hex">dec2hex</a>, <a class="ref" href="#XREFbase2dec">base2dec</a>, <a class="ref" href="#XREFbin2dec">bin2dec</a>.
</p></dd></dl>
<a class="anchor" id="XREFdec2base"></a><span style="display:block; margin-top:-4.5ex;"> </span>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-dec2base"><span><code class="def-type"><var class="var">str</var> =</code> <strong class="def-name">dec2base</strong> <code class="def-code-arguments">(<var class="var">d</var>, <var class="var">base</var>)</code><a class="copiable-link" href="#index-dec2base"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-dec2base-1"><span><code class="def-type"><var class="var">str</var> =</code> <strong class="def-name">dec2base</strong> <code class="def-code-arguments">(<var class="var">d</var>, <var class="var">base</var>, <var class="var">len</var>)</code><a class="copiable-link" href="#index-dec2base-1"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-dec2base-2"><span><code class="def-type"><var class="var">str</var> =</code> <strong class="def-name">dec2base</strong> <code class="def-code-arguments">(<var class="var">d</var>, <var class="var">base</var>, <var class="var">len</var>, <var class="var">decimals</var>)</code><a class="copiable-link" href="#index-dec2base-2"> ¶</a></span></dt>
<dd><p>Return a string of symbols in base <var class="var">base</var> corresponding to the
value <var class="var">d</var>.
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">dec2base (123, 3)
⇒ "11120"
</pre></div></div>
<p>If <var class="var">d</var> is negative, then the result will represent <var class="var">d</var> in complement
notation. For example, negative binary numbers are in twos-complement, and
analogously for other bases.
</p>
<p>If <var class="var">d</var> is a matrix or cell array, return a string matrix with one row
per element in <var class="var">d</var>, padded with leading zeros to the width of the
largest value.
</p>
<p>If <var class="var">base</var> is a string then the characters of <var class="var">base</var> are used as
the symbols for the digits of <var class="var">d</var>. Whitespace (spaces, tabs, newlines,
, etc.) may not be used as a symbol.
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">dec2base (123, "aei")
⇒ "eeeia"
</pre></div></div>
<p>The optional third argument, <var class="var">len</var>, specifies the minimum number of
digits in the integer part of the result. If this is omitted, then
<code class="code">dec2base</code> uses enough digits to accommodate the input.
</p>
<p>The optional fourth argument, <var class="var">decimals</var>, specifies the number of
digits to represent the fractional part of the input. If this is omitted,
then it is set to zero, and <code class="code">dec2base</code> returns an integer output for
backward compatibility.
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">dec2base (100*pi, 16)
⇒ "13A"
dec2base (100*pi, 16, 4)
⇒ "013A"
dec2base (100*pi, 16, 4, 6)
⇒ "013A.28C59D"
dec2base (-100*pi, 16)
⇒ "EC6"
dec2base (-100*pi, 16, 4)
⇒ "FEC6"
dec2base (-100*pi, 16, 4, 6)
⇒ "FEC5.D73A63"
</pre></div></div>
<p>Programming tip: When passing negative inputs to <code class="code">dec2base</code>, it is
best to explicitly specify the length of the output required.
</p>
<p><strong class="strong">See also:</strong> <a class="ref" href="#XREFbase2dec">base2dec</a>, <a class="ref" href="#XREFdec2bin">dec2bin</a>, <a class="ref" href="#XREFdec2hex">dec2hex</a>.
</p></dd></dl>
<a class="anchor" id="XREFbase2dec"></a><span style="display:block; margin-top:-4.5ex;"> </span>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-base2dec"><span><code class="def-type"><var class="var">d</var> =</code> <strong class="def-name">base2dec</strong> <code class="def-code-arguments">(<var class="var">str</var>, <var class="var">base</var>)</code><a class="copiable-link" href="#index-base2dec"> ¶</a></span></dt>
<dd><p>Convert <var class="var">str</var> from a string of digits in base <var class="var">base</var> to a decimal
integer (base 10).
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">base2dec ("11120", 3)
⇒ 123
</pre></div></div>
<p>If <var class="var">str</var> is a string matrix, return a column vector with one value per
row of <var class="var">str</var>. If a row contains invalid symbols then the corresponding
value will be NaN.
</p>
<p>If <var class="var">str</var> is a cell array of strings, return a column vector with one
value per cell element in <var class="var">str</var>.
</p>
<p>If <var class="var">base</var> is a string, the characters of <var class="var">base</var> are used as the
symbols for the digits of <var class="var">str</var>. Space (’ ’) may not be used as a
symbol.
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">base2dec ("yyyzx", "xyz")
⇒ 123
</pre></div></div>
<p><strong class="strong">See also:</strong> <a class="ref" href="#XREFdec2base">dec2base</a>, <a class="ref" href="#XREFbin2dec">bin2dec</a>, <a class="ref" href="#XREFhex2dec">hex2dec</a>.
</p></dd></dl>
<a class="anchor" id="XREFnum2hex"></a><span style="display:block; margin-top:-4.5ex;"> </span>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-num2hex"><span><code class="def-type"><var class="var">s</var> =</code> <strong class="def-name">num2hex</strong> <code class="def-code-arguments">(<var class="var">n</var>)</code><a class="copiable-link" href="#index-num2hex"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-num2hex-1"><span><code class="def-type"><var class="var">s</var> =</code> <strong class="def-name">num2hex</strong> <code class="def-code-arguments">(<var class="var">n</var>, "cell")</code><a class="copiable-link" href="#index-num2hex-1"> ¶</a></span></dt>
<dd><p>Convert a numeric array to an array of hexadecimal strings.
</p>
<p>For example:
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">num2hex ([-1, 1, e, Inf])
⇒ "bff0000000000000
3ff0000000000000
4005bf0a8b145769
7ff0000000000000"
</pre></div></div>
<p>If the argument <var class="var">n</var> is a single precision number or vector, the returned
string has a length of 8. For example:
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">num2hex (single ([-1, 1, e, Inf]))
⇒ "bf800000
3f800000
402df854
7f800000"
</pre></div></div>
<p>With the optional second argument <code class="code">"cell"</code>, return a cell array of
strings instead of a character array.
</p>
<p><strong class="strong">See also:</strong> <a class="ref" href="#XREFhex2num">hex2num</a>, <a class="ref" href="#XREFhex2dec">hex2dec</a>, <a class="ref" href="#XREFdec2hex">dec2hex</a>.
</p></dd></dl>
<a class="anchor" id="XREFhex2num"></a><span style="display:block; margin-top:-4.5ex;"> </span>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-hex2num"><span><code class="def-type"><var class="var">n</var> =</code> <strong class="def-name">hex2num</strong> <code class="def-code-arguments">(<var class="var">s</var>)</code><a class="copiable-link" href="#index-hex2num"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-hex2num-1"><span><code class="def-type"><var class="var">n</var> =</code> <strong class="def-name">hex2num</strong> <code class="def-code-arguments">(<var class="var">s</var>, <var class="var">class</var>)</code><a class="copiable-link" href="#index-hex2num-1"> ¶</a></span></dt>
<dd><p>Typecast a hexadecimal character array or cell array of strings to an
array of numbers.
</p>
<p>By default, the input array is interpreted as a hexadecimal number
representing a double precision value. If fewer than 16 characters are
given the strings are right padded with <code class="code">'0'</code> characters.
</p>
<p>Given a string matrix, <code class="code">hex2num</code> treats each row as a separate number.
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">hex2num (["4005bf0a8b145769"; "4024000000000000"])
⇒ [2.7183; 10.000]
</pre></div></div>
<p>The optional second argument <var class="var">class</var> may be used to cause the input
array to be interpreted as a different value type. Possible values are
</p>
<table class="multitable">
<thead><tr><th>Option</th><th>Characters</th></tr></thead>
<tbody><tr><td><code class="code">"int8"</code></td><td>2</td></tr>
<tr><td><code class="code">"uint8"</code></td><td>2</td></tr>
<tr><td><code class="code">"int16"</code></td><td>4</td></tr>
<tr><td><code class="code">"uint16"</code></td><td>4</td></tr>
<tr><td><code class="code">"int32"</code></td><td>8</td></tr>
<tr><td><code class="code">"uint32"</code></td><td>8</td></tr>
<tr><td><code class="code">"int64"</code></td><td>16</td></tr>
<tr><td><code class="code">"uint64"</code></td><td>16</td></tr>
<tr><td><code class="code">"char"</code></td><td>2</td></tr>
<tr><td><code class="code">"single"</code></td><td>8</td></tr>
<tr><td><code class="code">"double"</code></td><td>16</td></tr>
</tbody>
</table>
<p>For example:
</p>
<div class="example">
<div class="group"><pre class="example-preformatted">hex2num (["402df854"; "41200000"], "single")
⇒ [2.7183; 10.000]
</pre></div></div>
<p><strong class="strong">See also:</strong> <a class="ref" href="#XREFnum2hex">num2hex</a>, <a class="ref" href="#XREFhex2dec">hex2dec</a>, <a class="ref" href="#XREFdec2hex">dec2hex</a>.
</p></dd></dl>
<a class="anchor" id="XREFstrread"></a><span style="display:block; margin-top:-4.5ex;"> </span>
<dl class="first-deftypefn">
<dt class="deftypefn" id="index-strread"><span><code class="def-type">[<var class="var">a</var>, …] =</code> <strong class="def-name">strread</strong> <code class="def-code-arguments">(<var class="var">str</var>)</code><a class="copiable-link" href="#index-strread"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-strread-1"><span><code class="def-type">[<var class="var">a</var>, …] =</code> <strong class="def-name">strread</strong> <code class="def-code-arguments">(<var class="var">str</var>, <var class="var">format</var>)</code><a class="copiable-link" href="#index-strread-1"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-strread-2"><span><code class="def-type">[<var class="var">a</var>, …] =</code> <strong class="def-name">strread</strong> <code class="def-code-arguments">(<var class="var">str</var>, <var class="var">format</var>, <var class="var">format_repeat</var>)</code><a class="copiable-link" href="#index-strread-2"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-strread-3"><span><code class="def-type">[<var class="var">a</var>, …] =</code> <strong class="def-name">strread</strong> <code class="def-code-arguments">(<var class="var">str</var>, <var class="var">format</var>, <var class="var">prop1</var>, <var class="var">value1</var>, …)</code><a class="copiable-link" href="#index-strread-3"> ¶</a></span></dt>
<dt class="deftypefnx def-cmd-deftypefn" id="index-strread-4"><span><code class="def-type">[<var class="var">a</var>, …] =</code> <strong class="def-name">strread</strong> <code class="def-code-arguments">(<var class="var">str</var>, <var class="var">format</var>, <var class="var">format_repeat</var>, <var class="var">prop1</var>, <var class="var">value1</var>, …)</code><a class="copiable-link" href="#index-strread-4"> ¶</a></span></dt>
<dd>
<p>This function is obsolete. Use <code class="code">textscan</code> instead.
</p>
<p>Read data from a string.
</p>
<p>The string <var class="var">str</var> is split into words that are repeatedly matched to the
specifiers in <var class="var">format</var>. The first word is matched to the first
specifier, the second to the second specifier and so forth. If there are
more words than specifiers, the process is repeated until all words have
been processed.
</p>
<p>The string <var class="var">format</var> describes how the words in <var class="var">str</var> should be
parsed. It may contain any combination of the following specifiers:
</p>
<dl class="table">
<dt><code class="code">%s</code></dt>
<dd><p>The word is parsed as a string.
</p>
</dd>
<dt><code class="code">%f</code></dt>
<dt><code class="code">%n</code></dt>
<dd><p>The word is parsed as a number and converted to double.
</p>
</dd>
<dt><code class="code">%d</code></dt>
<dt><code class="code">%u</code></dt>
<dd><p>The word is parsed as a number and converted to int32.
</p>
</dd>
<dt><code class="code">%*</code></dt>
<dt><code class="code">%*f</code></dt>
<dt><code class="code">%*s</code></dt>
<dd><p>The word is skipped.
</p>
<p>For %s and %d, %f, %n, %u and the associated %*s … specifiers an
optional width can be specified as %Ns, etc. where N is an integer > 1.
For %f, format specifiers like %N.Mf are allowed.
</p>
</dd>
<dt><code class="code">literals</code></dt>
<dd><p>In addition the format may contain literal character strings; these will be
skipped during reading.
</p></dd>
</dl>
<p>Parsed word corresponding to the first specifier are returned in the first
output argument and likewise for the rest of the specifiers.
</p>
<p>By default, <var class="var">format</var> is <code class="t">"%f"</code>, meaning that numbers are read from
<var class="var">str</var>. This will do if <var class="var">str</var> contains only numeric fields.
</p>
<p>For example, the string
</p>
<div class="example">
<div class="group"><pre class="example-preformatted"><var class="var">str</var> = "\
Bunny Bugs 5.5\n\
Duck Daffy -7.5e-5\n\
Penguin Tux 6"
</pre></div></div>
<p>can be read using
</p>
<div class="example">
<pre class="example-preformatted">[<var class="var">a</var>, <var class="var">b</var>, <var class="var">c</var>] = strread (<var class="var">str</var>, "%s %s %f");
</pre></div>
<p>Optional numeric argument <var class="var">format_repeat</var> can be used for limiting the
number of items read:
</p>
<dl class="table">
<dt>-1</dt>
<dd><p>(default) read all of the string until the end.
</p>
</dd>
<dt>N</dt>
<dd><p>Read N times <var class="var">nargout</var> items. 0 (zero) is an acceptable value for
<var class="var">format_repeat</var>.
</p></dd>
</dl>
<p>The behavior of <code class="code">strread</code> can be changed via property-value pairs. The
following properties are recognized:
</p>
<dl class="table">
<dt><code class="code">"commentstyle"</code></dt>
<dd><p>Parts of <var class="var">str</var> are considered comments and will be skipped.
<var class="var">value</var> is the comment style and can be any of the following.
</p>
<ul class="itemize mark-bullet">
<li><code class="code">"shell"</code>
Everything from <code class="code">#</code> characters to the nearest end-of-line is skipped.
</li><li><code class="code">"c"</code>
Everything between <code class="code">/*</code> and <code class="code">*/</code> is skipped.
</li><li><code class="code">"c++"</code>
Everything from <code class="code">//</code> characters to the nearest end-of-line is skipped.
</li><li><code class="code">"matlab"</code>
Everything from <code class="code">%</code> characters to the nearest end-of-line is skipped.
</li><li>user-supplied. Two options:
(1) One string, or 1x1 cell string: Skip everything to the right of it;
(2) 2x1 cell string array: Everything between the left and right strings
is skipped.
</li></ul>
</dd>
<dt><code class="code">"delimiter"</code></dt>
<dd><p>Any character in <var class="var">value</var> will be used to split <var class="var">str</var> into words
(default value = any whitespace). Note that whitespace is implicitly added
to the set of delimiter characters unless a <code class="code">"%s"</code> format conversion
specifier is supplied; see <code class="code">"whitespace"</code> parameter below. The set
of delimiter characters cannot be empty; if needed Octave substitutes a
space as delimiter.
</p>
</dd>
<dt><code class="code">"emptyvalue"</code></dt>
<dd><p>Value to return for empty numeric values in non-whitespace delimited data.
The default is NaN. When the data type does not support NaN (int32 for
example), then default is zero.
</p>
</dd>
<dt><code class="code">"multipledelimsasone"</code></dt>
<dd><p>Treat a series of consecutive delimiters, without whitespace in between,
as a single delimiter. Consecutive delimiter series need not be vertically
<code class="code">"aligned"</code>.
</p>
</dd>
<dt><code class="code">"treatasempty"</code></dt>
<dd><p>Treat single occurrences (surrounded by delimiters or whitespace) of the
string(s) in <var class="var">value</var> as missing values.
</p>
</dd>
<dt><code class="code">"returnonerror"</code></dt>
<dd><p>If <var class="var">value</var> true (1, default), ignore read errors and return normally.
If false (0), return an error.
</p>
</dd>
<dt><code class="code">"whitespace"</code></dt>
<dd><p>Any character in <var class="var">value</var> will be interpreted as whitespace and trimmed;
the string defining whitespace must be enclosed in double quotes for proper
processing of special characters like <code class="code">"\t"</code>. In
each data field, multiple consecutive whitespace characters are collapsed
into one space and leading and trailing whitespace is removed. The default
value for whitespace is
<code class="code">"
\b\r\n\t"</code>
(note the space). Whitespace is always added to the set of delimiter
characters unless at least one <code class="code">"%s"</code> format conversion specifier is
supplied; in that case only whitespace explicitly specified in
<code class="code">"delimiter"</code> is retained as delimiter and removed from the set of
whitespace characters. If whitespace characters are to be kept as-is (in
e.g., strings), specify an empty value (i.e., <code class="code">""</code>) for
<code class="code">"whitespace"</code>; obviously, whitespace cannot be a delimiter then.
</p>
</dd>
</dl>
<p>When the number of words in <var class="var">str</var> doesn’t match an exact multiple of
the number of format conversion specifiers, strread’s behavior depends on
the last character of <var class="var">str</var>:
</p>
<dl class="table">
<dt>last character = <code class="code">"\n"</code></dt>
<dd><p>Data columns are padded with empty fields or NaN so that all columns have
equal length
</p>
</dd>
<dt>last character is not <code class="code">"\n"</code></dt>
<dd><p>Data columns are not padded; strread returns columns of unequal length
</p>
</dd>
</dl>
<p><strong class="strong">See also:</strong> <a class="ref" href="Simple-File-I_002fO.html#XREFtextscan">textscan</a>, <a class="ref" href="Formatted-Input.html#XREFsscanf">sscanf</a>.
</p></dd></dl>
</div>
<hr>
<div class="nav-panel">
<p>
Next: <a href="JSON-data-encoding_002fdecoding.html">JSON data encoding/decoding</a>, Previous: <a href="String-encoding.html">String encoding</a>, Up: <a href="Converting-Strings.html">Converting Strings</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>
|