matlab导入CSV文件

2024-12-19 08:03:05
推荐回答(2个)
回答1:

先说一下字符串,字符串在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.

回答2:

菜单里Files -> Import data...

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