how to make a 2 files .mat become 1?

4 次查看(过去 30 天)
Veizal Rasyid
Veizal Rasyid 2017-8-19
编辑: Jan 2017-8-23
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 个评论
Jan
Jan 2017-8-22
@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(:).'];

请先登录,再进行评论。

回答(4 个)

Image Analyst
Image Analyst 2017-8-19
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 个评论
Image Analyst
Image Analyst 2017-8-22
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
Adam 2017-8-22
编辑:Adam 2017-8-22
"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.

请先登录,再进行评论。


Stephen23
Stephen23 2017-8-20
编辑:Stephen23 2017-8-23
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 个评论
Adam
Adam 2017-8-23
'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
Jan 2017-8-23
编辑:Jan 2017-8-23
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.

请先登录,再进行评论。


Jan
Jan 2017-8-20
编辑:Jan 2017-8-20
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 2017-8-20
编辑:John BG 2017-8-20
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 个评论
Walter Roberson
Walter Roberson 2017-8-23
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};

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Performance and Memory 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by