(In the original version of the post) You are assigning n_i x 2 values into a single element of the output array.
There are two ways to concatenate all the arrays, one is much more performant than the other.
The better performing version uses the cell array to first load all the arrays, as you do in the revised question:
arraydata = cell(1,M); % preallocate the array!
for i = 1:M
file_path = fullfile(basepath, samplenames{i}, 'temp.mat');
% Load the data from the constructed file path
loaded_data = load(file_path);
data1 = loaded_data.mymatrix.'; # saved mtx are of dim n_i x 2, transposed to 2 x n_i
arraydata{i} = data1;
end
arraydata = [arraydata{:}]; % concatenate horizontally
The other, simpler but much slower way is:
arraydata = [];
for i = 1:M
file_path = fullfile(basepath, samplenames{i}, 'temp.mat');
% Load the data from the constructed file path
loaded_data = load(file_path);
data1 = loaded_data.mymatrix.'; # saved mtx are of dim n_i x 2, transposed to 2 x n_i
arraydata = [arraydata,data1]; % concatenate horizontally
end
Note that you could also load the data a bit more simply as load(file_path,'mymatrix'), without assigning to anything, which will create the variable mymatrix. Specifying which array to load in this way can save a lot of time if the MAT-files contain more data than the array you want to load.
In recent versions of MATLAB you can also apply dot indexing directly on the function call:
data1 = load(file_path,'mymatrix').mymatrix.';
2 x (n_1 + n_2 + ... + n_N)array? It would help if you edited your question to make the code into a minimal reproducible example we can actually run using arbitrary values (e.g. usingrand()) since we don't have your mat files, then you can illustrate the expected output