카테고리 없음

Selection of Files and Directories

오늘보다 나은 내일 2014. 8. 29. 11:28

Standard dialog boxes to select files or directory locations for loading and saving data as you know them from other applications are also available in Matlab.

help

All of them have a large number of configuration options, which cannot be discussed in detail here, thus only some typical application examples are shown. For further options, please consult the Help System.

Select a file: uigetfile

[FileName, PathName] = uigetfile('*.m', 'Select the M-file');

Opens the file selection dialog box. The function returns the file name and the path. If «Cancel» has been clicked, FileName = 0 results.

The first argument '*.m' is a filter, i.e. only files with this extension are shown. If all files should be visible, '*.*' can be entered. The second argument contains the text that will be shown in the window title.
The selection process begins at the current path as it is set in Matlab as the «Current Directory».

If you want the selection to start at a different place in the file system, you can specify it like this:

[FileName,PathName] = ...
uigetfile('d:\matlab\data\*.txt', 'Select data set');

If several file types should be available, you can specify this by means of a cell array. The desired file extensions are listed in the first column of the cell array:

[filename, pathname] = ...
uigetfile({'*.m'; '*.mdl'; '*.mat'; '*.*'}, 'File Selector');

act

Example for a file selection dialog with output of the result. Copy the code lines below into the Command Window (>> can also be copied - Matlab will ignore them).

>> [filename, pathname] = uigetfile('*.txt', 'Pick a text file');
>> if isequal(filename, 0)
>> disp('User selected ''Cancel''')
>> else
>> disp(['User selected ', fullfile(pathname, filename)])
>> end

Resulting file selection windowResulting file selection window

Select a directory: uigetdir

directory_name = uigetdir('c:\data\', 'Please select the directory')

Opens a directory selection dialog box directory_name contains the selected path. If the user hits «Cancel», the value 0 is returned.
The first argument designates the path where the selection process starts out. The second argument holds the window title text.

Directory selection windowDirectory selection window

Select a directory/file name to save a file: uiputfile

[FileName, PathName] = uiputfile('*.dat', 'Save as...')
[FileName, PathName] = uiputfile('vp27data.dat', 'Save as...')

Creates a dialog box to select the location for a file saving operation. The function returns the selected file name and path. In case «Cancel» has been pressed, FileName = 0 results.
The first argument specifies the file type; in the second example, a complete file name is suggested. The second argument again specifies the window title text.

Window to select the location to save a fileWindow to select the location to save a file

Two useful functions to handle file and path names

Often, the queried file names have to be disassembled into their parts, or a complete path has to be assembled from parts. To aid this, there are helpful functions:

act

Please try it out for yourself.

Decompose a complete path into its constituents:fileparts

>> [pathstr, name, ext] = fileparts('c:\data\new\myfile.txt')

And the converse: Generate a complete path from parts: fullfile

>> filepath = fullfile(pathstr, [name ext])
>> filepath = fullfile('C:', 'data', 'new', 'myfile.txt')