how to concatenate mat files of different row sizes but same column size vertically?

2 views (last 30 days)
i am trying to concatenate 8 different matfiles in one newly created matfile. all matfiles have fixed column size but row sizes vary.
but getting this error:
Error using vertcat
Dimensions of matrices being
concatenated are not
consistent.
here is my code:
a = load('TwoD_fv_roi_rangefilt_bdip');
b = load('TwoD_fv_roi_rangefilt_bilateral');
c = load('TwoD_fv_roi_rangefilt_cdf');
d = load('TwoD_fv_roi_rangefilt_GradOrient');
e = load('TwoD_fv_roi_rangefilt_hog');
f = load('TwoD_fv_roi_rangefilt_lbp');
g = load('TwoD_fv_roi_rangefilt_rglbp');
h = load('TwoD_fv_roi_rangefilt_ssgm');
Fv_ROI_concatenated = nan(50,421); %create new mat file
Fv_ROI_concatenated = [a.TwoD_fv_roi_rangefilt_bdip,b.TwoD_fv_roi_rangefilt_bilateral
,c.TwoD_fv_roi_rangefilt_cdf, d.TwoD_fv_roi_rangefilt_GradOrient, e.TwoD_fv_roi_rangefilt_hog
,f.TwoD_fv_roi_rangefilt_lbp,g.TwoD_fv_roi_rangefilt_rglbp, h.TwoD_fv_roi_rangefilt_ssgm ];
save Fv_ROI_concatenated;

Answers (2)

Massimo Zanetti
Massimo Zanetti on 17 Jan 2017
In this line of code:
Fv_ROI_concatenated = [a.TwoD_fv_roi_rangefilt_bdip,b.TwoD_fv_roi_rangefilt_bilateral,c.TwoD_fv_roi_rangefilt_cdf, d.TwoD_fv_roi_rangefilt_GradOrient, e.TwoD_fv_roi_rangefilt_hog,
f.TwoD_fv_roi_rangefilt_lbp,g.TwoD_fv_roi_rangefilt_rglbp, h.TwoD_fv_roi_rangefilt_ssgm ];
try replacing all commas with semicolons :)

Guillaume
Guillaume on 17 Jan 2017
Please use the {} Code button to format your code as code.
I'm unclear from your question whether it's the number of rows or the number of columns that is the same for all matrices. Furthermore, the error you get is not consistent with the code you've shown. You're using horizontal concatenation in the code you've posted (horzcat, because of the ,) whereas the error message would indicate that the actual code used vertical concatenation (vertcat, using ; to separate the matrices).
Anyway, you've got a programming language available that is very good at repeating tasks for you, such as loading 8 (or 30 or 100) different matrices. Take advantage of that:
filestoload = {'TwoD_fv_roi_rangefilt_bdip'
'TwoD_fv_roi_rangefilt_bilateral'
'TwoD_fv_roi_rangefilt_cdf'
'TwoD_fv_roi_rangefilt_GradOrient'
'TwoD_fv_roi_rangefilt_hog'
'TwoD_fv_roi_rangefilt_lbp'
'TwoD_fv_roi_rangefilt_rglbp'
'TwoD_fv_roi_rangefilt_ssgm'};
filescontent = cell(size(filestoload)); %cell array to hold each file content
for fidx = 1:numel(filestoload)
m = load(filestoload{fidx});
filescontent{fidx} = m.(filestoload{fidx});
end
This will load all the files, however many there are with you only having to change the file list.
You can then ask matlab to concatenate them along their common dimension:
[nrows, ncols] = cellfun(@size, filescontent);
if all(nrows == nrows(1)) %same number of rows
concatenatedfiles = horzcat(filescontent{:}); %concatenate horizontally
elseif all(ncols == ncols(1)) %same number of columns
concatenatedfiles = vertcat(filescontent{:});
else
error('no common dimension');
end

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!