1

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=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

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

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

2 Comments

i as variable should be avoided. It is used as the complex number > i shows it: 0.0000 + 1.0000i
@JonasStein: right but you also could argue the opposite of never using i as the imaginary unit, instead always use 1i whenever you are referring to the complex number, thus avoiding any confusion.. Either way, this was discussed before :)
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.