0

I am running a matlab script that extracts data and saves them in variables based upon variable input - see code below:

folder = 'Raw/';
fileStart = 'DAT_MT_';
fileMiddle = '_M1_';
fileEnd = '.csv';
years = cellstr(years);

price = zeros(0,1);
date = zeros(0,1);
time = zeros(0,1);

for l = 1:length(years)
    string = strcat(folder,fileStart,currency,fileMiddle,years(l),fileEnd);
    string = char(string);

    T = readtable(string,'ReadVariableNames',false);
    U = T(:,[1,2,6]);

    p = table2array(U(:,3));
    d = table2array(U(:,1));
    t = table2array(U(:,2));
    price = vertcat(price,p);
    date = vertcat(date,d);
    time = vertcat(time,t);
end

The code works fine and delivers restults as expected, however it is very slow due to the preallocation issue. Usually I solve this problem by initializing the variable with zeros(). However, to my knowledge this only works if you know the final size of the array. For my script, this is not the case, because the input is variable and hence the final size of the array is unknown. Is there any way to boost performance of such a code without knowing the final size of the array?

Many thanks upfront!

Benvaulter
  • 249
  • 1
  • 5
  • 14
  • 1
    Everything is the same size as `years`. Just initialise zero matrices using `length(years)`, just as your `for`-loop. – Adriaan Sep 02 '15 at 11:56
  • the length of the years only refers to a varying amount of csv files to examine and extract data from, hence this is unfortunately not the case. For example, I might input 3 different years and then the for loop will cycle through 3 csv files and then construct the data for each file. As I want one complete set of data, I then use `vertcat` to attach the data - however, this is what is slowing done the code – Benvaulter Sep 02 '15 at 12:43

0 Answers0