当前位置:网站首页>Matlab数据导入--importdata和load函数
Matlab数据导入--importdata和load函数
2022-07-27 08:41:00 【用户9925864】
importdata和load函数
1.引言
在使用matlab将数据导入到工作空间的时候,经常会使用到两个函数,一个是importdata函数,另一个是load函数,它们的使用方法和使用场景是太相同的,如果不太注意就可能会犯错误,在这里做简要的说明和记录。
2.importdata函数
下面是帮助文档提供的importdata函数的语法说明
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函数主要特点是可以从文本文件中导入数据
- 导入的数据可以是类似于表格形式的,可以含有表头即列名称,也可以不含表头。
- 表头可以是文本形式的。
- 除了官方文档说明的这些我发现,importdata导入的数据除了可以列名外还可以含有行名。导入时列名会被放在colheaders数组中,行名会被放在rowheaders数组中。
- importdata还可以用来导入图片,传入图片的名称即可
样例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 =
包含以下字段的 struct:
data: [5×4 double]
textdata: {'a1' 'a2' 'a3' 'a4'}
colheaders: {'a1' 'a2' 'a3' 'a4'}
>> class(impdata)
ans =
'struct'可见importdata函数会将数据导入到一个结构体变量中,文本与数字分别导入到结构不同的属性中。数字部分导入到属性data中是一个矩阵,文本部分导入属性textdata中是一个细胞数组。如果textdata中的文本是列名则同时还会保存到属性colheaders中
样例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}如果textdata中的文本是行名则同时还会保存到属性rowheaders中。注意不能将行名称放在最后一列,这样将不能正确导入。所以在进行分类时,不能将字符型的类标签放在最后一列,而是应该放在第一列。
样例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'}可以看到如果在头部文本超过两行,只有最后一行会作为列名分开读取,前面的列作为一个字符串,不分开,分隔符也不会被识别,而是当做普通字符。
样例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函数
load函数一般将用来导入纯数字的文件,可以是文本格式的文件或者是matlab保存的mat格式的文件。
>> A = magic(3)
A =
8 1 6
3 5 7
4 9 2
>> save A.mat A
>> clear
>> load A.mat
>> load 5.txt # 自动生成X+txt文件名的变量
>> B = load('5.txt') # 把导入的数值赋给B边栏推荐
- Vertical align cannot align the picture and text vertically
- sql_ Mode strict mode (ansi/traditional/strict_trans_tables)
- Flask login implementation
- I drew a Gu ailing with characters!
- NIO总结文——一篇读懂NIO整个流程
- 说透缓存一致性与内存屏障
- Interviewer: what is scaffolding? Why do you need scaffolding? What are the commonly used scaffolds?
- Do a reptile project by yourself
- 开怀一笑
- Creation and simple application of QPushButton button button
猜你喜欢

Explain cache consistency and memory barrier

微信安装包从0.5M暴涨到260M,为什么我们的程序越来越大?

4276. 擅长C

Node installation and debugging

第2章 前台数据展现

OSI seven layer model and tcp/ip four layer (TCP and UDP) (notes)

VS Code中#include报错(新建的头文件)

4274. 后缀表达式

Oppo self-developed large-scale knowledge map and its application in digital intelligence engineering

View 的滑动冲突
随机推荐
Forced login, seven cattle cloud upload pictures
Flutter 渲染机制——GPU线程渲染
说透缓存一致性与内存屏障
Login to homepage function implementation
Do a reptile project by yourself
How to view instances of software objects in QSIM?
ROS2安装时出现Connection failed [IP: 91.189.91.39 80]
海关总署:这类产品暂停进口
OSI seven layer model and tcp/ip four layer (TCP and UDP) (notes)
4279. 笛卡尔树
It's better to be full than delicious; It's better to be drunk than drunk
第2章 前台数据展现
Breadth first search
海量数据肖枫:共建共治openGauss根社区,共享欣欣向荣新生态
JS basic exercises
Have a good laugh
Blueprint class view method
General view, DRF view review
Installation and use of Supervisor
3311. Longest arithmetic