Matlab跳过过热导入文本文件(后续)
我希望能够导入一个包含46行标题和4列x6数据的文件。我试着使用上一个答案中的这个例子,但是文本扫描不起作用 这是我的测试文件:ImportTest.txt 1.02.03.0 1.1 2.1 3.1 1.2.2.3.3 这是代码。标准数据大小不同。为什么?Matlab跳过过热导入文本文件(后续),matlab,Matlab,我希望能够导入一个包含46行标题和4列x6数据的文件。我试着使用上一个答案中的这个例子,但是文本扫描不起作用 这是我的测试文件:ImportTest.txt 1.02.03.0 1.1 2.1 3.1 1.2.2.3.3 这是代码。标准数据大小不同。为什么? %open file fid = fopen('ImportTest.txt'); strData = textscan(fid,'%s%s%s%s', 'Delimiter',',') fclose(fid); %# catenate,
%open file
fid = fopen('ImportTest.txt');
strData = textscan(fid,'%s%s%s%s', 'Delimiter',',')
fclose(fid);
%# catenate, b/c textscan returns a column of cells for each column in the data
strData = cat(2,strData{:}) ;
%# convert cols 3:6 to double
doubleData = str2double(strData(:,3:end));
%# find header rows. headerRows is a logical array
headerRowsL = all(isnan(doubleData),2);
%# since I guess you know what the headers are, you can just remove the header rows
dateAndTimeCell = strData(~headerRowsL,1:2);
dataArray = doubleData(~headerRowsL,:);
%# and you're ready to start working with your data
您可以使用
dlmread
功能读取输入文件:
header_rows=5;
delim_char=' ';
C=dlmread('ImportTest.txt',delim_char,header_rows,0)
在调用中,您可以指定要跳过的标题行的nummember(示例中为header_行)和分隔符字符(示例中为delim_char)
调用中的最后一个参数(0)定义数据开始的列
从文本文件读取的数据直接存储在数组中(示例中为C)
给定此输入文件(具有5个标题行):
输出将是:
C =
1.0000 2.0000 3.0000
1.1000 2.1000 3.1000
1.2000 2.2000 3.3000
或者,您可以使用importdata
header_rows=5;
delim_char=' ';
c=importdata('ImportTest.txt',delim_char,header_rows)
在这种情况下,输出将是一个结构:
c =
data: [3x3 double]
textdata: {5x3 cell}
colheaders: {'header' 'line' '5'}
数据存储在“数据”字段中,“文本数据”字段中的5个标题行中
最后一个标题行也被解释为数据的实际标题
希望这有帮助
Qapla'
c =
data: [3x3 double]
textdata: {5x3 cell}
colheaders: {'header' 'line' '5'}