当前位置:网站首页>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边栏推荐
- Arm system call exception assembly
- Make a game by yourself with pyGame 01
- Oppo self-developed large-scale knowledge map and its application in digital intelligence engineering
- 海关总署:这类产品暂停进口
- Function realization of order system
- 说透缓存一致性与内存屏障
- Installation and use of Supervisor
- Use of elastic box / expansion box (Flex)
- 如何卸载--奇安信安全终端管理系统
- Realize SKU management in the background
猜你喜欢

Create a project to realize login and registration, generate JWT, and send verification code

View 的滑动冲突

Day5 - Flame restful request response and Sqlalchemy Foundation

E. Split into two sets

Bandwidth and currency

Flutter 渲染机制——GPU线程渲染

永久设置source的方法

The following license SolidWorks Standard cannot be obtained, and the use license file cannot be found. (-1,359,2)。

Functions and arrow functions

regular expression
随机推荐
[penetration test tool sharing] [dnslog server building guidance]
Software test interview questions (key points)
Background order management
Flask login implementation
【nonebot2】几个简单的机器人模块(一言+彩虹屁+每日60s)
海量数据肖枫:共建共治openGauss根社区,共享欣欣向荣新生态
03.使用引号来监听对象嵌套值的变化
4277. 区块反转
Massive data Xiao Feng: jointly build and govern opengauss root community and share a thriving new ecosystem
JS rotation chart
Day5 - Flame restful request response and Sqlalchemy Foundation
NIO示例
ROS2安装时出现Connection failed [IP: 91.189.91.39 80]
MySQL Express
regular expression
QPushButton 按钮的创建与简单应用
Flask's operations on model classes
MCDF顶层验证方案
带宽 与 货币
Creation and simple application of QPushButton button button