I am working with a meteorological dataset and need to extract one column from many csv files and compile the results into a new file. I have it working through one month but the script gets stuck when it encounters a shorter month (Subscripted assignment dimension
mismatch). Makes sense, however, I want it to simply keep the NaN values that are originally present in the placeholder matrix D.
Here is the problem part of the script,
%Convert dates to matlab date numbers and get number of rows
Date = datenum(Date{1, 1}, 'dd-mm-yyyy');
T = size(Date, 1);
%# Preallocate a matrix to hold all the data, and add the date column
D = [Date, NaN(T, NumFile)];
%# Loop over the csv files, get the eleventh column and add it to the data matrix
for k = 1:NumFile
FileList = dir('*.csv');
NumFile = size(FileList,1);
filename = FileList(k).name;
disp(filename);
%# Get the current file name
CurFilePath = filename;
%# Open the current file for reading and scan in the second column using numerical format
fid1 = fopen(CurFilePath, 'r');
CurData = textscan(fid1, '%*s %*s %*s %*s %*s %*s %*s %*s %f %*[^\n]', 'Delimiter', ',"', 'HeaderLines', 17, 'MultipleDelimsAsOne',true);
fclose(fid1);
%Add the current data to the cell array
D(:, k+1) = CurData{1, 1};
end
So, how can the shorter months be forced to the size of 31 days months to fit the placeholder matrix D.
When you are assigning
Dwith the colon operator in one dimension, Matlab has to assume that you are assigning all elements in the row. To fix it, simply exchange the colon with a1:numberOfDaysInMonth.That way Matlab will only assign the number of values you specify and leave the rest unchanged, Nan in this case.numberOfDaysInMonthyou can calculate assize(CurData{1, 1},1)In all, exchange that next-to-last line in your script with: