将多个文件中的数据附加到matlab数组

时间:2011-09-19 21:35:43

标签: matlab file-io matrix append

您好我需要您帮助将我从许多文件中读取的所有数据附加到矩阵中。我制作了以下脚本

path='C:\Users\Kostas\Documents\MATLAB\';
filefolder=strcat(path,'MSL*.txt');
files=dir(filefolder);
k=0;
for i=1:length(files)
    filename=strcat(path,files(i).name);    
    %load the filename and create vectors of height (Z),
    %lat and lon
    newData=importdata(filename,'\t', 1);
    vars = fieldnames(newData);
for j = 1:length(vars)
    assignin('base', vars{j}, newData.(vars{j}));
end 
    timeas=data(:,1);
    lat=data(:,2);
    lon=data(:,3);
    Z=data(:,4);
  %  daten=(timeas/24)+doy;
   k=k+1; 
%append data to matrix Teff_series
    Teff_series(k,:)= [timeas lat lon Z];
end

我运行此脚本时收到的错误消息是

??? Subscripted assignment dimension mismatch.

Error in ==> te at 31
    Teff_series(k,:)= [lat lon Z];

提前致谢

2 个答案:

答案 0 :(得分:2)

让我举个例子:

%# get the list of files
fpath = 'C:\Users\Amro\Desktop\';
files = dir( fullfile(fpath,'file*.dat') );
files = strcat(fpath,{files.name}');

%# read data from all files and store in cell array
Teff_series = cell(numel(files),1);
for i=1:numel(files)
    newData = importdata(files{i}, '\t', 1);
    Teff_series{i} = newData.data;
end

%# combine all into a matrix
data = vertcat(Teff_series{:});
colNames = newData.colheaders;

%# extract columns as vectors
t = data(:,1);
lat = data(:,2);
lon = data(:,3);
Z = data(:,4);

如果我使用这些样本数据文件:

file1.dat

t   lat lon Z
1   2   3   4
2   3   4   5
4   5   6   6

File2.DAT的

t   lat lon Z
4   5   6   6
2   3   4   5
1   2   3   4

我得到以下结果:

>> colNames
colNames = 
    't'    'lat'    'lon'    'Z'

>> data
data =
     1     2     3     4
     2     3     4     5
     4     5     6     6
    40    50    60    60
    20    30    40    50
    10    20    30    40

答案 1 :(得分:1)

错误表示相等表达式的左侧 - 在这种情况下:     Teff_series(k,:))

的大小与右手大小不同:

[lat lon Z]

调试此问题的一种方法是执行命令:

dbstop if all error

然后重新运行您的脚本。它将在抛出错误的位置停止调试器,然后您可以找出大小的差异。

希望这有帮助。