MATLAB Answers

0

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);
I=imread(file1);
imhist(I)
[n,xout]=imhist(I); %nilai piksel
featureSize=size(n);
featuresN=reshape(n,1,featureSize(1)); %jumlah piksel
featureSize=size(xout);
featuresXout=reshape(xout,1,featureSize(1)); %save ke databasefitur
databaseFitur2=[featuresN;featuresXout];
save('databaseFitur2','databaseFitur2');

  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.

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.

  3 Comments

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.
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.


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');
copyfile(D(1).name,'ZZZ.mat')
for k = 2:numel(D)
S = load(D(k).name);
save('ZZZ.mat','-struct','S','-append')
end
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.

  16 Comments

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.
Adam
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.
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')
and:
load('file1.mat')
load('file2.mat')
matobj1=matfile('file1.mat')
matobj2=matfile('file2.mat')
L1=who(matobj1)'
L2=who(matobj2)'
L={L1{:} L2{:}}
save('file3.mat',L{:})
? 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'.

  0 Comments

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:
1.
load the mat files in the workspace
load('file1.mat')
load('file2.mat')
2.
get the handles of each input mat file
matobj1=matfile('file1.mat')
matobj2=matfile('file2.mat')
3.
get the names of the variables contained in each input mat file
L1=who(matobj1)'
L2=who(matobj2)'
4.
built a complete list of variable names to save in resulting mat file
L={L1{:} L2{:}}
5.
save all variables in output mat file
save('file3.mat',L{:})
6.
check
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 = {L1.name, L2.name};

Sign in to comment.