how to make a 2 files .mat become 1?

3 views (last 30 days)
Veizal Rasyid
Veizal Rasyid on 19 Aug 2017
Edited: 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
Jan 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.

Answers (4)

Image Analyst
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.
Adam on 22 Aug 2017
Edited: Adam 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.

Stephen23 on 20 Aug 2017
Edited: Stephen23 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.
Jan on 23 Aug 2017
Edited: Jan 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.

Jan on 20 Aug 2017
Edited: 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'.

John BG
John BG on 20 Aug 2017
Edited: 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
Walter Roberson
Walter Roberson on 23 Aug 2017
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.

Community Treasure Hunt

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

Start Hunting!