当前位置:网站首页>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边栏推荐
- JS basic exercises
- 杭州电子商务研究院发布“数字化存在”新名词解释
- Flink1.15 source code reading Flink clients client execution process (reading is boring)
- sql_ Mode strict mode (ansi/traditional/strict_trans_tables)
- JS rotation chart
- Solution of database migration error
- Flask one to many database creation, basic addition, deletion, modification and query
- Create a project to realize login and registration, generate JWT, and send verification code
- while Loop
- 微信安装包从0.5M暴涨到260M,为什么我们的程序越来越大?
猜你喜欢

openGauss之TryMe初体验

Have a good laugh

Day4 --- flask blueprint and rest ful

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

First experience of tryme in opengauss

Solution to the program design of the sequence structure of one book (Chapter 1)

Hundreds of people participated. What are these people talking about in the opengauss open source community?

Eval and assert execute one sentence Trojan horse

如何在qsim查看软件对象的实例?

说透缓存一致性与内存屏障
随机推荐
Luogu Taotao picks apples
MySQL Express
Management of product pictures
Day5 - Flame restful request response and Sqlalchemy Foundation
regular expression
3428. 放苹果
Flink1.15 source code reading Flink clients client execution process (reading is boring)
4275. Dijkstra序列
OPPO 自研大规模知识图谱及其在数智工程中的应用
【uni-app高级实战】手把手带你学习一个纯实战复杂项目的开发1/100
redis的string类型及bitmap
List delete collection elements
Day6 --- Sqlalchemy advanced
Flutter 渲染机制——GPU线程渲染
Have a good laugh
MCDF top level verification scheme
Openresty + keepalived 实现负载均衡 + IPV6 验证
Node installation and debugging
691. 立方体IV
arguments