MATLAB Programming
MATLAB Programming MATLAB Programming
6 Data Import and Export Example of Selecting Variables to Load. Use the Import Wizard to import this sample binary MAT-file, my_data.mat, C = 1 2 3 4 5 6 7 8 9 10 D = a test string The Import Wizard displays two variables, as listed in the preview pane. To select a variable to import, select the check box next to its name. All variables are preselected by default. Preview of the variables in the file. Preview of the data in each variable. Automated M-Code Generation To perform additional imports from this or a similar type of file, you can automate this process by creating a MATLAB function that performs all of the steps you just went through. To have the Import Wizard write this function 6-16
Using the Import Wizard for you, select the Generate M-code checkbox in the lower right corner of the Wizard dialog. Once you click Finish to complete the import, MATLAB opens an Editor window displaying the generated M-file function. The function is called importfile.m. If this name is already taken, then MATLAB names the file importfileN.m, whereN is a number that is one greater than the highest existing importfile.m file. The generated function has the following input and output arguments: • Input: fileToRead1—Name of the file to import from. This argument exists only when importing from a file. • Output: newData1—Structure to assign all imported data to. This argument exists only if you have specified an output argument with the call to uiimport when starting the Import Wizard. Otherwise, variables retain the same naming as assigned within the Wizard. The newData1 output is a structure that has one field for each output of the import operation. The workspace variables created by this generated M-code are the same as those created by running the Import Wizard. For example, if you elect to format the output in column vectors when running the Import Wizard, the generated M-file does the same. However, unlike the Import Wizard, you cannot mark any variables to be excluded from the output. Make any necessary modifications to the generated M-file function in the Editor window. To save the M-file, select Save from the File menu at the top. Caution You must save the file yourself; MATLAB does not automatically save it for you. Example of M-Code Generation TheM-fileshownbelowwasgenerated by MATLAB during an import of the file grades.txt, shown earlier in this section. During the import that created this file, the option to Create vectors from each row using row names 6-17
- Page 404 and 405: 5 Types of Functions Note Function
- Page 406 and 407: 5 Types of Functions Arrays of Anon
- Page 408 and 409: 5 Types of Functions Display the da
- Page 410 and 411: 5 Types of Functions If you change
- Page 412 and 413: 5 Types of Functions • “Example
- Page 414 and 415: 5 Types of Functions Supply the val
- Page 416 and 417: 5 Types of Functions Nested Functio
- Page 418 and 419: 5 Types of Functions • From a fun
- Page 420 and 421: 5 Types of Functions not shared bet
- Page 422 and 423: 5 Types of Functions You can now us
- Page 424 and 425: 5 Types of Functions Using a Subfun
- Page 426 and 427: 5 Types of Functions count = 125 22
- Page 428 and 429: 5 Types of Functions shape. The mak
- Page 430 and 431: 5 Types of Functions % FILTFCN. Eac
- Page 432 and 433: 5 Types of Functions 0.6667 filt2(1
- Page 434 and 435: 5 Types of Functions unless you dec
- Page 436 and 437: 5 Types of Functions Accessing Help
- Page 438 and 439: 5 Types of Functions 5-38
- Page 440 and 441: 6 Data Import and Export Working wi
- Page 442 and 443: 6 Data Import and Export • “Dat
- Page 444 and 445: 6 Data Import and Export file that
- Page 446 and 447: 6 Data Import and Export Function r
- Page 448 and 449: 6 Data Import and Export Importing
- Page 450 and 451: 6 Data Import and Export • rowhea
- Page 452 and 453: 6 Data Import and Export File to im
- Page 456 and 457: 6 Data Import and Export was select
- Page 458 and 459: 6 Data Import and Export Import fro
- Page 460 and 461: 6 Data Import and Export File Forma
- Page 462 and 463: 6 Data Import and Export or save ju
- Page 464 and 465: 6 Data Import and Export Data Compr
- Page 466 and 467: 6 Data Import and Export Preference
- Page 468 and 469: 6 Data Import and Export Saving fro
- Page 470 and 471: 6 Data Import and Export Loading Bi
- Page 472 and 473: 6 Data Import and Export Accessing
- 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
Using the Import Wizard<br />
for you, select the Generate M-code checkbox in the lower right corner of<br />
the Wizard dialog.<br />
Once you click Finish to complete the import, <strong>MATLAB</strong> opens an Editor<br />
window displaying the generated M-file function. The function is called<br />
importfile.m. If this name is already taken, then <strong>MATLAB</strong> names the file<br />
importfileN.m, whereN is a number that is one greater than the highest<br />
existing importfile.m file.<br />
The generated function has the following input and output arguments:<br />
• Input: fileToRead1—Name of the file to import from. This argument<br />
exists only when importing from a file.<br />
• Output: newData1—Structure to assign all imported data to. This<br />
argument exists only if you have specified an output argument with the<br />
call to uiimport when starting the Import Wizard. Otherwise, variables<br />
retain the same naming as assigned within the Wizard.<br />
The newData1 output is a structure that has one field for each output of the<br />
import operation.<br />
The workspace variables created by this generated M-code are the same as<br />
those created by running the Import Wizard. For example, if you elect to<br />
format the output in column vectors when running the Import Wizard, the<br />
generated M-file does the same. However, unlike the Import Wizard, you<br />
cannot mark any variables to be excluded from the output.<br />
Make any necessary modifications to the generated M-file function in the<br />
Editor window. To save the M-file, select Save from the File menu at the top.<br />
Caution You must save the file yourself; <strong>MATLAB</strong> does not automatically<br />
save it for you.<br />
Example of M-Code Generation<br />
TheM-fileshownbelowwasgenerated by <strong>MATLAB</strong> during an import of the<br />
file grades.txt, shown earlier in this section. During the import that created<br />
this file, the option to Create vectors from each row using row names<br />
6-17