当前位置:网站首页>MATLAB data import -- importdata and load functions
MATLAB data import -- importdata and load functions
2022-07-27 08:47:00 【User 9925864】
importdata and load function
1. introduction
In the use of matlab When importing data into the workspace , Two functions are often used , One is importdata function , The other is load function , Their usage methods and usage scenarios are too same , If you don't pay much attention, you may make mistakes , Here is a brief description and record .
2.importdata function
The following is what the help document provides importdata Syntax description of function
importdata load data from file
Syntax A = importdata(filename) A = importdata(‘-pastespecial’) A = importdata(_,delimiterIn) A = importdata(_,delimiterIn,headerlinesIn) [A,delimiterOut,headerlinesOut] = importdata(_)
Description - A = importdata(filename) loads data into array A. - 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. - A = importdata(_,delimiterIn,headerlinesIn) loads data from ASCII file, filename, or the clipboard, reading numeric data starting from line headerlinesIn+1. - [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.
- importdata The main feature of the function is that it can import data from text files
- The imported data can be similar to the table form , It can contain header, namely column name , You can also exclude the header .
- The header can be in the form of text .
- In addition to the official documents, I found ,importdata The imported data can contain row names in addition to column names . When importing, the column name will be placed in colheaders Array , The row name will be placed in rowheaders Array .
- importdata It can also be used to import pictures , Just pass in the name of the picture
Examples 1:
%1.txt
%a1 a2 a3 a4
%1 3 5 3
%1 2 4 6
%7 5 4 8
%3 7 9 8
%2 3 7 4
>> impdata = importdata('1.txt')
impdata =
That contains the following fields struct:
data: [5×4 double]
textdata: {'a1' 'a2' 'a3' 'a4'}
colheaders: {'a1' 'a2' 'a3' 'a4'}
>> class(impdata)
ans =
'struct'so importdata Function will import data into a structural variable , Text and numbers are imported into attributes with different structures . The numeric part is imported into the attribute data Is a matrix , Text part import properties textdata Is an array of cells . If textdata If the text in is the column name, it will also be saved to the attribute colheaders in
Examples 2:
%2.txt
%b1 1 3 5 3
%b2 1 2 4 6
%b3 7 5 4 8
%b4 3 7 9 8
%b5 2 3 7 4
>> impdata = importdata('2.txt')
impdata =
data: [5x4 double]
textdata: {5x1 cell}
rowheaders: {5x1 cell}If textdata If the text in is the row name, it will also be saved to the attribute rowheaders in . Note that row names cannot be placed in the last column , This will not import correctly . So when classifying , Character type class labels cannot be placed in the last column , It should be placed in the first column .
Examples 3:
%3.txt
%a1 a2 a3 a4
%b1 b2 b3 b4
%1 3 5 3
%1 2 4 6
%7 5 4 8
%3 7 9 8
%2 3 7 4
>> impdata = importdata('3.txt')
impdata =
data: [5x4 double]
textdata: {2x4 cell}
colheaders: {'b1' 'b2' 'b3' 'b4'}You can see if the text in the header is more than two lines , Only the last row will be read separately as column names , The preceding column is treated as a string , Don't separate , Delimiters are not recognized , But as ordinary characters .
Examples 4:
%a1 a2 a3 a4
%b1 1 3 5
%b2 1 2 4
%b3 7 5 4
%b4 3 7 9
%b5 2 3 7
>> impdata = importdata('4.txt')
impdata =
data: [5x3 double]
textdata: {6x4 cell}3.load function
load Function is generally used to import pure digital files , It can be a text file or matlab The saved mat File format .
>> A = magic(3)
A =
8 1 6
3 5 7
4 9 2
>> save A.mat A
>> clear
>> load A.mat
>> load 5.txt # Automatic generation X+txt Variable of file name
>> B = load('5.txt') # Assign the imported value to B边栏推荐
- NiO example
- Day5 - Flame restful request response and Sqlalchemy Foundation
- Cookie addition, deletion, modification and exception
- “鼓浪屿元宇宙”,能否成为中国文旅产业的“升级样本”
- [I2C reading mpu6050 of Renesas ra6m4 development board]
- Flask one to many database creation, basic addition, deletion, modification and query
- How to permanently set source
- 无法获取下列许可SOLIDWORKS Standard,无法找到使用许可文件。(-1,359,2)。
- Explain cache consistency and memory barrier
- 4274. 后缀表达式
猜你喜欢

MySQL Express

Block, there is a gap between the block elements in the row

数智革新

After downloading URL loader and specifying the size of the image with limit, the image will not be displayed

Forced login, seven cattle cloud upload pictures

How to permanently set source

UVM入门实验1
![2040: [Blue Bridge Cup 2022 preliminary] bamboo cutting (priority queue)](/img/76/512b7fd4db55f9f7d8f5bcb646d9fc.jpg)
2040: [Blue Bridge Cup 2022 preliminary] bamboo cutting (priority queue)
![ROS2安装时出现Connection failed [IP: 91.189.91.39 80]](/img/7f/92b7d44cddc03c58364d8d3f19198a.png)
ROS2安装时出现Connection failed [IP: 91.189.91.39 80]

Digital intelligence innovation
随机推荐
众昂矿业:新能源行业快速发展,氟化工产品势头强劲
4276. 擅长C
1.3.1 Full Permutation Problem
Blueprint class view method
Minio 安装与使用
How to permanently set source
3311. 最长算术
Openresty + keepalived 实现负载均衡 + IPV6 验证
4279. Cartesian tree
4275. Dijkstra序列
How to upload qiniu cloud
General Administration of Customs: the import of such products is suspended
Unity3D 2021软件安装包下载及安装教程
4274. Suffix expression
Redis network IO
CMD command and NPM command
NiO Summary - read and understand the whole NiO process
2036: [Blue Bridge Cup 2022 preliminary] statistical submatrix (two-dimensional prefix sum, one-dimensional prefix sum)
MCDF top level verification scheme
Is online account opening safe? Want to know how securities companies get preferential accounts?