Append data from multiple files to matlab array
Hello i need you help to append all the data i read from many files into a matrix. I have made the following script
path='C:\Users\Kostas\Documents\MATLAB\';
filefolder=strcat(path,'MSL*.txt');
files=dir(filefolder);
k=开发者_如何学JAVA0;
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
the error message i get when i run this script is
??? Subscripted assignment dimension mismatch.
Error in ==> te at 31
Teff_series(k,:)= [lat lon Z];
Thanks in advance
Let me give an example:
%# 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);
If I use these sample data files:
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
I get the following results:
>> 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
The error indicates that the left hand side of the equal expression - in this case: Teff_series(k, :)
is of a different size than the right hand side:
[lat lon Z]
One way to debug this issue is execute the command:
dbstop if all error
and then re-run your script. It will stop the debugger at the point where the error is thrown and then you can figure out the difference in sizes.
Hope this helps.
精彩评论