MATLAB Programming
MATLAB Programming MATLAB Programming
6 Data Import and Export A = [ 1 2 3 4 ; 5 6 7 8 ]; use the save function, as follows: save my_data.out A -ASCII If you view the created file in a text editor, it looks like this: 1.0000000e+000 2.0000000e+000 3.0000000e+000 4.0000000e+000 5.0000000e+000 6.0000000e+000 7.0000000e+000 8.0000000e+000 By default, save uses spaces as delimiters but you can use tabs instead of spaces by specifying the -tabs option. When you use save to write a character array to an ASCII file, it writes the ASCII equivalent of the characters to the file. If you write the character string 'hello' to a file, save writes the values 104 101 108 108 111 Using the dlmwrite Function To export an array in ASCII format and specify the delimiter used in the file, use the dlmwrite function. For example, to export the array A, A = [ 1 2 3 4 ; 5 6 7 8 ]; as an ASCII data file that uses semicolons as a delimiter, use this command: dlmwrite('my_data.out',A, ';') If you view the created file in a text editor, it looks like this: 1;2;3;4 5;6;7;8 Note that dlmwrite does not insert delimiters at the end of rows. 6-86
Exporting Text Data By default, if you do not specify a delimiter, dlmwrite uses a comma as a delimiter. You can specify a space (' ') as a delimiter or, if you specify empty quotes (''), no delimiter. Using the diary Function to Export Data To export small numeric arrays or cell arrays, you can use the diary function. diary creates a verbatim copy of your MATLAB session in a disk file (excluding graphics). For example, if you have the array A in your workspace, A = [ 1 2 3 4; 5 6 7 8 ]; execute these commands at the MATLAB prompt to export this array using diary: 1 Turn on the diary function. You can optionally name the output file diary creates. diary my_data.out 2 Display the contents of the array you want to export. This example displays the array A. You could also display a cell array or other MATLAB data type. A = 1 2 3 4 5 6 7 8 3 Turn off the diary function. diary off diary creates the file my_data.out and records all the commands executed in the MATLAB session until it is turned off. A = 1 2 3 4 5 6 7 8 6-87
- Page 474 and 475: 6 Data Import and Export Sharing Me
- Page 476 and 477: 6 Data Import and Export • For sm
- Page 478 and 479: 6 Data Import and Export Note Prope
- Page 480 and 481: 6 Data Import and Export Writable:
- Page 482 and 483: 6 Data Import and Export Ifthefilet
- Page 484 and 485: 6 Data Import and Export For exampl
- Page 486 and 487: 6 Data Import and Export m = memmap
- Page 488 and 489: 6 Data Import and Export Mapping of
- Page 490 and 491: 6 Data Import and Export Repeating
- Page 492 and 493: 6 Data Import and Export Note To su
- Page 494 and 495: 6 Data Import and Export d = m.Data
- Page 496 and 497: 6 Data Import and Export ans = 166x
- Page 498 and 499: 6 Data Import and Export If you hav
- Page 500 and 501: 6 Data Import and Export m.Data(60)
- Page 502 and 503: 6 Data Import and Export m = memmap
- Page 504 and 505: 6 Data Import and Export d = m.Data
- Page 506 and 507: 6 Data Import and Export Data: 1658
- Page 508 and 509: 6 Data Import and Export One MATLAB
- Page 510 and 511: 6 Data Import and Export disp('ANSW
- Page 512 and 513: 6 Data Import and Export Terminatin
- Page 514 and 515: 6 Data Import and Export character.
- Page 516 and 517: 6 Data Import and Export Table6-2 A
- Page 518 and 519: 6 Data Import and Export A = dlmrea
- Page 520 and 521: 6 Data Import and Export number of
- Page 522 and 523: 6 Data Import and Export Exporting
- Page 526 and 527: 6 Data Import and Export diary off
- Page 528 and 529: 6 Data Import and Export Importing
- Page 530 and 531: 6 Data Import and Export Working wi
- Page 532 and 533: 6 Data Import and Export Exporting
- Page 534 and 535: 6 Data Import and Export 3 Close th
- Page 536 and 537: 6 Data Import and Export type = Mic
- Page 538 and 539: 6 Data Import and Export ndata = xl
- Page 540 and 541: 6 Data Import and Export 41 42 43 4
- Page 542 and 543: 6 Data Import and Export Note While
- Page 544 and 545: 6 Data Import and Export Use these
- Page 546 and 547: 6 Data Import and Export Writing Bi
- Page 548 and 549: 6 Data Import and Export The act of
- Page 550 and 551: 6 Data Import and Export Conversion
- Page 552 and 553: 6 Data Import and Export • Two sp
- Page 554 and 555: 6 Data Import and Export Exchanging
- Page 556 and 557: 6 Data Import and Export urlread('h
- Page 558 and 559: 6 Data Import and Export Once you h
- Page 560 and 561: 6 Data Import and Export For exampl
- Page 562 and 563: 6 Data Import and Export 6-124
- Page 564 and 565: 7 Working with Scientific Data Form
- Page 566 and 567: 7 Working with Scientific Data Form
- Page 568 and 569: 7 Working with Scientific Data Form
- Page 570 and 571: 7 Working with Scientific Data Form
- Page 572 and 573: 7 Working with Scientific Data Form
6 Data Import and Export<br />
A = [ 1 2 3 4 ; 5 6 7 8 ];<br />
use the save function, as follows:<br />
save my_data.out A -ASCII<br />
If you view the created file in a text editor, it looks like this:<br />
1.0000000e+000 2.0000000e+000 3.0000000e+000 4.0000000e+000<br />
5.0000000e+000 6.0000000e+000 7.0000000e+000 8.0000000e+000<br />
By default, save uses spaces as delimiters but you can use tabs instead of<br />
spaces by specifying the -tabs option.<br />
When you use save to write a character array to an ASCII file, it writes the<br />
ASCII equivalent of the characters to the file. If you write the character string<br />
'hello' to a file, save writes the values<br />
104 101 108 108 111<br />
Using the dlmwrite Function<br />
To export an array in ASCII format and specify the delimiter used in the file,<br />
use the dlmwrite function.<br />
For example, to export the array A,<br />
A = [ 1 2 3 4 ; 5 6 7 8 ];<br />
as an ASCII data file that uses semicolons as a delimiter, use this command:<br />
dlmwrite('my_data.out',A, ';')<br />
If you view the created file in a text editor, it looks like this:<br />
1;2;3;4<br />
5;6;7;8<br />
Note that dlmwrite does not insert delimiters at the end of rows.<br />
6-86