MATLAB Programming

MATLAB Programming MATLAB Programming

cda.psych.uiuc.edu
from cda.psych.uiuc.edu More from this publisher
23.06.2015 Views

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

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

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!