23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

4 M-File <strong>Programming</strong><br />

On the other hand, variables that contain strings do not need to be enclosed<br />

in quotes:<br />

dirname = 'myapptests';<br />

mkdir(dirname)<br />

Passing Filenames<br />

You can specify a filename argument using the <strong>MATLAB</strong> command or function<br />

syntax. For example, either of the following are acceptable. (The .mat file<br />

extension is optional for save and load):<br />

load mydata.mat<br />

load('mydata.mat')<br />

% Command syntax<br />

% Function syntax<br />

Ifyouassigntheoutputtoavariable,youmustusethefunctionsyntax:<br />

savedData = load('mydata.mat')<br />

Specify ASCII files as shown here. In this case, the file extension is required:<br />

load mydata.dat -ascii<br />

load('mydata.dat','-ascii')<br />

% Command syntax<br />

% Function syntax<br />

Determining Filenames at Run-Time. There are several ways that your<br />

function code can work on specific files without your having to hardcode their<br />

filenames into the program. You can<br />

• Passthefilenameasanargument:<br />

function myfun(datafile)<br />

• Prompt for the filename using the input function:<br />

filename = input('Enter name of file:<br />

', 's');<br />

• Browse for the file using the uigetfile function:<br />

[filename, pathname] = uigetfile('*.mat', 'Select MAT-file');<br />

4-62

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

Saved successfully!

Ooh no, something went wrong!