MATLAB Answers


how to make a 2 files .mat become 1?

Asked by Veizal Rasyid on 19 Aug 2017
Latest activity Edited by Jan
on 23 Aug 2017
i want to make 2 .mat files become 1 file. For example, I have 1 mat file when I give name databasefitur, and databasefitur1. In this case databasefitur and databasefitur1 have 2 rows and 255 columns. I want to make them as a group.
For example, I use this code in my program :
load file_name.mat
file1=strcat('..\Project Tugas Akhir\Histogram LoG\',file_name);
[n,xout]=imhist(I); %nilai piksel
featuresN=reshape(n,1,featureSize(1)); %jumlah piksel
featuresXout=reshape(xout,1,featureSize(1)); %save ke databasefitur

  1 Comment

on 22 Aug 2017
@Veizal: Sorry, your thread has been hijacked by a discussion, which does not help to solve your problem. You can ignore these comments without loosing any detail.
Note that the output array can be created easier:
[n, xout] = imhist(I);
databaseFitur2 = [n(:).'; xout(:).'];

Sign in to comment.

4 Answers

Answer by Image Analyst
on 19 Aug 2017

I don't quite understand the question, but in general, to save variables in a .mat file, you give the filename and follow with variable names enclosed in single quotes:
matFullFileName = fullfile(someFolder, 'databaseFiture.mat'); % someFolder could be pwd if you want it in the current folder.
% Save both databaseFitur1 and databaseFitur2 variables in file databaseFitur.mat.
save(matFullFileName, 'databaseFitur1', 'databaseFitur2');
or whatever.


my question is "How to merge 2 or more file .mat?"
You obviously already know how to call load() since you did it (though I would have assigned the return value to a structure like the others are also recommending). All that was missing was how to combine variables in a third .mat file. You did that incorrectly by forgetting to include the output filename as the first argument to save(), so I showed you how to do that. Just pass in the filename, followed by variables in single quotes, regardless of how you got them -- like from calling load to pull them out of another .mat file or by creating brand new variables or both -- and all those variables will be combined in your new .mat file.
on 22 Aug 2017
"advanced programming comments that is not needed to solve this question"
Your answer is 8 lines of code including the functions 'who' and 'matfile' as well as creating a cell array.
Stephen's code is 3 lines, 2 of which are the highly intuitive and un-complicated calls to 'load' and 'save'. People vote for answers for a reason, and that is before we even consider all the other good reasons for doing it that way.
Tagging your answer on to the answer which currently happens to be at the top of the list through being upvoted is no better than spamming adverts in random threads or answers.

Sign in to comment.

Answer by Stephen Cobeldick on 20 Aug 2017
Edited by Stephen Cobeldick on 23 Aug 2017

Actually it is really easy to efficiently merge two (or more) .mat files into one .mat file (to get the set union of the variables): the trick is to load and save a structure (which you should always be doing anyway!). First lets create some fake data and save it in two .mat files:
>> A = 'aaa';
>> B = 1:3;
>> save A.mat A
>> save B.mat B
We can now efficiently merge the contents of the two .mat files together:
>> copyfile('A.mat','C.mat') % C will be the merged file
>> S = load('B.mat');
>> save('C.mat','-struct','S','-append')
That is all!
Now C.mat contains all of the fields of A.mat and B.mat. Lets have a look:
>> new = load('C.mat')
new =
A: 'aaa'
B: [1 2 3]
It could even be done in a loop if you have multiple .mat files to merge, something like this (untested):
D = dir('*.mat');
for k = 2:numel(D)
S = load(D(k).name);
Note that you should never simply load into the workspace (always load into a structure), and using very inefficient introspective commands, like who or whos, is not required for this task.


Please any one help me solve this question by choosing my answer as the correct answer and I will owe you one correct answer. To use a popular term these days: That's collusion!
On a comedy level, it's fun to think that OP might never read this and has probably moved on with his life.
on 23 Aug 2017
'You are telling Veizal to use a structure, but there's no need for a structure'
Where does this notion of 'need' come from? Your answer tells him to write 8 or 10 lines of code using whos and matfile. He doesn't 'need' to use either to achieve his aim. Irrespective of any arguments of general good code, the use of a struct is incidental other than that it allows for a neat 3 line solution that doesn't require all those other lines of code. That is the simplest reason to use a struct - because it results in clear, short, easy to understand code.
on 23 Aug 2017
Sorry, do we really need to discuss the difference between
S = load('B.mat');
save('A.mat', '-struct', 'S', '-append')
L={L1{:} L2{:}}
? Beside the obvious advantages the problems of load without catching the output have been explained already.
This discussion provides time and attention to John BG, but does not help to solve the problem of the OP anymore.

Sign in to comment.

Answer by Jan
on 20 Aug 2017
Edited by Jan
on 20 Aug 2017

It depends on what you want to merge: The data or the saved fields.
  • Case 1: The data:
Data = rand(255, 2); % Some test data
save('YourFile.mat', 'Data');
Data = rand(255, 2); % Some test data
FileCont = load('YourFile.mat');
Data = cat(1, FileCont.Data, Data); % Merge the data
save('YourFile.mat', 'Data');
Now the file contains a [512 x 2] matrix.
  • Case 2: The fields:
Data1 = rand(255, 2); % Some test data
save('YourFile.mat', 'Data1');
Data2 = rand(255, 2); % Some test data
save('YourFile.mat', 'Data2', '-append');
Now the file contains the 2 fields 'Data1' and 'Data2'.


Sign in to comment.

Answer by John BG
on 20 Aug 2017
Edited by John BG
on 20 Aug 2017

Hi Veizal Rasyid
combining mat files into a single mat file is simple:
load the mat files in the workspace
get the handles of each input mat file
get the names of the variables contained in each input mat file
built a complete list of variable names to save in resulting mat file
L={L1{:} L2{:}}
save all variables in output mat file
clear all
load file3.mat
if you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?
To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link
thanks in advance
John BG

  1 Comment

Instead of using matfile() and who() to find the variable names, you could instead whos() the file:
L1 = whos('-file', 'file1.mat');
L2 = whos('-file', 'file2.mat');
L = {,};

Sign in to comment.