指点成金-最美分享吧

登录

matlab怎么把txt文档数据导入

佚名 举报

篇首语:本文由小编为大家整理,主要介绍了matlab怎么把txt文档数据导入相关的知识,希望对你有一定的参考价值。

先来看看txt文档中保存的数据结构,如图所示,数据之间用空格隔开,这种结构是比较理想的。可以直接导入。或者数据之间用逗号、分号、tab符号等等,都算作比较理想的。
接着我们打开MATLAB,在file菜单下找到import data选项
打开一个浏览窗口,找到你要导入的txt文件
找到txt文件以后,选中它,然后点击打开按钮,别着急,还没完。
打开数据导入向导,我们首先要选择是什么符号隔开了数据,这里自动的使用了空格作为间隔符,还可以使用comma(逗号)、semicolon(分号)、tab(四个空格)、其他符号。
接着,我们设置标题行,我们看下面的数据是没有标题的,所以设置成0
选择要导入的变量,我们这里只有一种,就选择这个就可以了,点击finish
这里就是我们导入的变量,在工作空间中可以看到。
参考技术A importdata根据文件名将数据导入到Matlab工作区。可以导入文件类型有很多,.txt .wav 等等。导入的数据(包括字符串和数值)以结构形式存放在工作区,可以使用whos命令来查看工作区的数据。
importdata可以导入load不能读取的长短不一的ASCII文件。当文件中既包含字符串又包含数值,而且数值长度个数不一时,可以使用importdata命令。注意读取的数值矩阵列数以文件中数值第一行的列数为标准。

例 test.txt
This is a test.
Start
0 1 2
1 2
1 2 3 4
运行A=importdata("test.txt")
结果:
A =
data: [4x3 double]
textdata: 2x1 cell

A.data =
0 1 2
1 2 NaN
1 2 3
4 NaN NaN

A.textdata =
"This is a test."
"Start"

另外,读取的字符串只能位于数值之前,位于数值之后的将被忽略。

例:test.txt
0 1 2
1 2
1 2 3 4
End.
运行A=importdata("test.txt")
结果:
A =
0 1 2
1 2 NaN
1 2 3
4 NaN NaN
参考技术B 有三种常见的方式:
1. A=importdata("filename.txt") 则A就是n*m的矩阵了;
2.load filename.txt 这样也是载入n*m的矩阵;
3.在MATLAB的work文件夹下,选择想要导入的数据,用右键importdata,根据向导一步一步导入即可。
参考技术C ①对于纯数字的行列整齐的文本,可以用load或importdata读取
②对于行列整齐但含有首行或首列字符串的文本,可以用importdata读取
③对于文件内容不是行列整齐的文本,或者其他条件导致不足以使用以上两条读取的情况,则只能使用流文件操作(fopen、fscanf、fgetl等)来逐行读取

matlab导入CSV文件

如何将CSV格式文件里的数据导入到matlab程序中?就是用matlab直接调用CSV里的数据~~~求具体语句~~~
尝试了import data,比如我插入的文档是DOW.txt,尝试了之后就会提示我
Error in ==> importfile at 9
newData1 = importdata(DOW.txt);

??? Undefined variable "DOW" or class "DOW.txt".

要在哪个地方定义dow呢?

这是matlab插入之后自动产生的:
function importfile(fileToRead1)
%IMPORTFILE(FILETOREAD1)
% Imports data from the specified file
% FILETOREAD1: file to read

% Auto-generated by MATLAB on 16-Apr-2008 17:59:56

% Import the file
newData1 = importdata(DOW.txt);

% Create new variables in the base workspace from those fields.
vars = fieldnames(newData1);
for i = 1:length(vars)
assignin("base", varsi, newData1.(varsi));
end

请问我要如何定义参数?

先说一下字符串,字符串在matlab中需要加单引号,你直接使用newData1 = importdata(DOW.txt);
matlab便会将DOW.txt看成是变量,但是importdata需要一个包含文件名的字符串变量,但是DOW.txt不知道是什么东西,所以会出现??? Undefined variable "DOW" or class "DOW.txt".

正确的使用方法是:
newData1 = importdata("DOW.txt");
或者
path="DOW.txt"
newData1 = importdata(path);

不过importdata不支持后缀名为txt文件,其支持的文件后缀有:
Data formats Command Returns
MAT - MATLAB workspace load Variables in file.
CSV - Comma separated numbers csvread Double array.
DAT - Formatted text importdata Double array.
DLM - Delimited text dlmread Double array.
TAB - Tab separated text dlmread Double array.

Spreadsheet formats
XLS - Excel worksheet xlsread Double array and cell array.
WK1 - Lotus 123 worksheet wk1read Double array and cell array.

Scientific data formats
CDF - Common Data Format cdfread Cell array of CDF records
FITS - Flexible Image Transport System fitsread Primary or extension table data
HDF - Hierarchical Data Format hdfread HDF or HDF-EOS data set

Movie formats
AVI - Movie aviread MATLAB movie.

Image formats
TIFF - TIFF image imread Truecolor, grayscale or indexed image(s).
PNG - PNG image imread Truecolor, grayscale or indexed image.
HDF - HDF image imread Truecolor or indexed image(s).
BMP - BMP image imread Truecolor or indexed image.
JPEG - JPEG image imread Truecolor or grayscale image.
GIF - GIF image imread Indexed image.
PCX - PCX image imread Indexed image.
XWD - XWD image imread Indexed image.
CUR - Cursor image imread Indexed image.
ICO - Icon image imread Indexed image.
RAS - Sun raster image imread Truecolor or indexed.
PBM - PBM image imread Grayscale image.
PGM - PGM image imread Grayscale image.
PPM - PPM image imread Truecolor image.

Audio formats
AU - NeXT/Sun sound auread Sound data and sample rate.
SND - NeXT/Sun sound auread Sound data and sample rate.
WAV - Microsoft Wave sound wavread Sound data and sample rate.

参考资料:matlab帮助文档

参考技术A 菜单里Files -> Import data...

你的csv文件是怎样的?csv文件里应该只有数据,没有其他任何东西。

以上是关于matlab怎么把txt文档数据导入的主要内容,如果未能解决你的问题,请参考以下文章