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!