Professional Documents
Culture Documents
MATLAB Data and File Management Data Import and Export Import and Export Basics
importdata
Load data from file
expand all in page
Syntax
A = importdata(filename)example A = importdata('-pastespecial')example A = importdata(___,delimiterIn) A = importdata(___,delimiterIn,headerlinesIn)example [A,delimiterOut,headerlinesOut] = importdata(___)example Description
example
A = importdata('-pastespecial') loads data from the system clipboard rather than from a file. A = importdata(___,delimiterIn) interprets delimiterIn as the column separator in ASCII file, filename, or the clipboard data. You can use delimiterIn with any of the input arguments in the above syntaxes.
example
A = importdata(___,delimiterIn,headerlinesIn) loads data from ASCII file, filename, or the clipboard, reading numeric data starting from line headerlinesIn+1.
example
[A,delimiterOut,headerlinesOut] = importdata(___) additionally returns the detected delimiter character for the input ASCII file in delimiterOut and the detected number of header lines in headerlinesOut, using any of the input arguments in the previous
syntaxes.
Examples
expand all
Import and Display an Image Import a Text File and Specify Delimiter and Column Header
Using a text editor, create a space-delimited ASCII file with column headers called myfile01.txt.
Day1
Day2
Day3
Day4
Day5
Day6
Day7 1.53
5.79 20.28
23.11 45.65 79.19 93.55 35.29 19.87 74.68 60.68 1.85 92.18 91.69 81.32 60.38 44.51 0.99 27.22 93.18
disp(' ')
end
Use textscan to load it and skip the other columns by using an asterick. fid = fopen('file.txt'); textscan(fid, '%*s%*s%s'); % loads only the third column fclose(fid); This assumes there are exactly three columns in your file. If you have many more columns, you will want: fid = fopen('file.txt'); twocols = textscan(fid,'%*s%*s%s%*[^\n]'); fclose(fid);
which results in all variables in use being saved in the file with the name `filename.mat'. The extension `.mat' does not necessary need to be given. If only a few variables need to be saved, these variable should be specified after the file name, e.g. only the variables saved in the file filename.mat with the command
>> save filename x y
and
are
A saved `.mat' file can be read into MATLAB with the command load
where the extension `.mat' can be omitted. The saved variables and their values are stored in the workspace and can now be used again. Besides loading data stored in `.mat' files, MATLAB is useful for processing data which is obtained from external sources, e.g., experimental measurements. Typically this data is available as a plain text file organized into columns. MATLAB can easily handle tab or space-delimited text. There is more than one way to read data into MATLAB from a external data file. The simplest, though least flexible, procedure is to use again the load command to read the entire content of the file in a single step. The load command requires that the data in the file is organized into a rectangular array. No column titles are permitted. One useful form of the load command is:
>> load measurements.txt
where `measurements.txt' is the name of the file containing the data. The result of this operation is that the data in `measurements.txt' is stored in a variable called `measurements'. Files with various extension can be used. Note, that any extension except `.mat' indicates to MATLAB that the data is stored as plain ASCII text. A `.mat' extension is reserved for a file which has stored MATLAB variables. Suppose you had a simple ASCII file named `meas_xy.txt' that contained two columns of numbers. The following MATLAB statements will load this data into the variable `meas_xy', and then copy it into two vectors,
>> load meas_xy.txt; >> x = meas_xy(:,1); >> y = meas_xy(:,2); % % %
and
read data into the meas_xy variable copy first column of my_xy into x and second column into y
After applying these commands, the data stored in column 1 of the text file is stored in the MATLAB variable MATLAB variable , and the data stored in column 2 of the text file is stored in the
window opening, you can select your data file. Selecting next in this window, various import options are accessable. For example, you can choose whether MATLAB should split columns between spaces, or between commas. Furthermore, you can choose the number of header rows. On the righthand side of your window, you will see a preview how MATLAB will import the data. If you are satisfied, select finish.
Modifying Properties
You can set and query annotation object properties using the set and get functions and the Property Editor (displayed with thepropertyeditor command). Use the annotation function to create annotation objects and obtain their handles. For an example of its use, see Annotation Objects in the MATLAB Graphics documentation.
Color ColorSpec
Text color. A three-element RGB vector or one of the predefined names, specifying the text color. The default value is black. SeeColorSpec for more information on specifying color.
FaceAlpha
Scalar alpha value in range [0 1] Transparency of object background. Defines the degree to which the object's background color is transparent. A value of 1 (the default) makes the background opaque, a value of 0 makes the background completely transparent (that is, invisible). The default value is 1.
FitHeightToText on | off
Automatically adjust text box width and height to fit text. MATLAB automatically wraps text strings to fit the width of the text box. However, if the text string is long enough, it can extend beyond the bottom of the text box. Note: The property is obsolete. To control line wrapping behavior in textboxes, use FitBoxToText instead.
When you set this mode to on, MATLAB automatically adjusts the height of the text box to accommodate the string, doing so as you create or edit the string.
The fit-size-to-text behavior turns off if you resize the text box programmatically or manually in plot edit mode.
However, if you resize the text box from any other handles, the position you set is honored without regard to how the text fits the box.
FontName
string Font family. A string specifying the name of the font to use for the text. To display and print properly, this font must be supported on your system. The default font is Helvetica.
FontSize
size in points Approximate size of text characters. Specify in points. 1 point = 1/72 inch. The default size is 10.
FontUnits {points} | normalized | inches | centimeters | pixels Font size units. MATLAB uses this property to determine the units used by the FontSize property. Normalized units interpret FontSizeas a fraction of the height of the parent axes. When you resize the axes, MATLAB modifies the screen FontSize accordingly. pixels,inches, centimeters, and points are absolute units. 1 point = / inch. FontWeight {normal} | bold | light | demi
1 72
Weight of text characters. MATLAB uses this property to select a font from those available on your system. Generally, setting this property to bold or demi causes MATLAB to use a bold font.
Horizontal alignment of text. Specifies the horizontal justification of the text string within the textbox. It determines where MATLAB places the string horizontally with regard to the points specified by the Position property.
Interpreter
latex | {tex} | none Interpret TeX instructions. This property controls whether MATLAB interprets certain characters in the String property as TeX instructions (default) or displays all characters literally. The options are:
latex Supports a basic subset of the LaTeX markup language. tex Supports a subset of plain TeX markup language. See the String property for a list of supported TeX instructions. none Displays literal characters. LineStyle {-} | -- | : | -. | none
Line style of annotation textbox object. Line Style Specifiers Table
LineWidth
size in points Width of linear objects and edges of filled areas. Specify in points. 1 point = 1/72 inch. The default is 0.5 points.
Margin
size in pixels Space around text. A value in pixels that defines the space around the text string, but within the rectangle. Default value is 5 pixels.
Position
four-element vector [x, y, width, height] Size and location of the object. Specify the lower left corner of the object with the first two elements of the vector defining the point x, y in units normalized to the figure (when Units property is normalized). width and height specify the object's dx and dy, respectively, in units normalized to the figure. The default value is [0.3 0.3 0.1 0.1].
String
string Text string. Specify this property as a quoted string for single-line strings, or as a cell array of strings, or a padded string matrix for multiline strings. MATLAB displays this string at the specified location. Vertical slash characters are not interpreted as line breaks in text strings, and are drawn as part of the text string. See Mathematical Symbols, Greek Letters, and TeX Characters for an example. Note: The words default, factory, and remove are reserved words that will not appear in a figure when quoted as a normal string. In order to display any of these words individually, type '\reserved_word' instead of 'reserved_word'. When the text Interpreter property is tex (the default), you can use a subset of TeX commands embedded in the string to produce special characters such as Greek letters and mathematical symbols. The following table lists these characters and the character sequences used to define them. TeX Character Sequence Table You can also specify stream modifiers that control font type and color. The first four modifiers are mutually exclusive. However, you can use \fontname in combination with one of the other modifiers.
Units {normalized} | inches | centimeters | characters | points | pixels Position units. MATLAB uses this property to determine the units used by the Position property. All positions are measured from
the lower left corner of the figure window.
normalized Interpret Position as a fraction of the width and height of the parent axes. When you resize the axes,
MATLAB modifies the size of the object accordingly.
pixels, inches, centimeters, and points Absolute units. 1 point = / inch. characters Based on the size of characters in the default system font. The width of one characters unit is the width of the letterx, and the height of one characters unit is the distance between the baselines of two lines of text. VerticalAlignment top | cap | {middle} | baseline | bottom
1 72
Vertical alignment of text. Specifies the vertical justification of the text string within the textbox. It determines where MATLAB places the string vertically with regard to the points specified by the Position property. Note that top and cap both place the text at the top of the box, while baseline and bottom both align the text on the bottom.
See Also
annotation
uiopen
Interactively select file to open and load data
expand all in page
Syntax
uiopen uiopen(type) uiopen(filename) uiopen(filename, TF) Description uiopen displays the dialog box with the file filter set to all MATLAB files (with file extensions *.m, *.mat, *.fig, *.mdl, and *.slx). uiopen(type) sets the file filter according to the type. uiopen(filename) displays filename as the default value for File name in the dialog box and lists only files having the same
extension.
uiopen(filename, TF) directly opens file filename without displaying a dialog box if TF is true, and displays the dialog box if TF is false. Input Arguments type
String that specifies the kind of file to show in the dialog box (the file filter). Acceptable values for type are the following.
Type string Files Displayed
All MATLAB files (with file extensions *.m, *.mat,*.fig, *.mdl, and *.slx.) All MAT-files (*.mat) All figure files (*.fig) All Simulink model files (*.mdl and *.slx) All MATLAB files except for .mat, .fig, and .slxfiles.
A string, including the file extension, naming a file to open. The filename can be a wildcard character plus extension. For example, *.txt displays a list of all files with the file extension .txt. A MATLAB expression that evaluates to true or false. If true, filename opens directly, without displaying the dialog.
Filter to display only FIG-files by setting the Files of type field to Figures (*.fig):
uiopen('figure')
Alternatives
In MATLAB code or in a command:
To open a file appropriately based on its file extension, use the open function. To open a file in the Editor, use the edit function. To open a MAT-file and load its contents into the workspace, use the load function. To open a FIG-file, use the openfig function. To open a file in an application in Microsoft Windows, use the winopen function.
More About