How to append two datas of two different .mat files into one .matfile?

One .matfiel contains one feature having 1x20 coefficient and another .matfile contain only one value. Then how to append this two into one .mat file?

Risposte (2)

It is very easy to efficiently create the union of two .mat files:
>> copyfile('A.mat','C.mat') % C will be the merged file
>> S = load('B.mat');
>> save('C.mat','-struct','S','-append')

8 Commenti

I tried the above program and that showing error.
The code is like this;
copyfile('c7i.mat','r77i.mat') % r77i will be the merged file
S = load('w7i.mat');
save('r77.mat','-struct','S','-append')
andthe error is this:
Error using save
Unable to write file r77.mat: No such file or directory.
Error in Untitled9 (line 9)
save('r77.mat','-struct','S','-append')
@Suchithra K S : you used two different filenames:
'r77i.mat'
'r77.mat'
If you look at my answer these are the same filename.
for appending 3 .matfiles what we have to do?
"for appending 3 .matfiles what we have to do?"
Repeat the second two lines as often as needed (hint: use a loop).
ok sir. Then there is any way to know the values in .matfile
"Then there is any way to know the values in .matfile"
load the file into MATLAB and look at the values.
"Repeat the second two lines as often as needed (hint: use a loop)."
is the below given code is ok or not
copyfile('c7i.mat','r77i.mat') % r77i will be the merged file
S = load('w7i.mat');
save('r77i.mat','-struct','S','-append')
S = load('t7i.mat');
save('r77i.mat','-struct','S','-append')
"is the below given code is ok or not"
If you are aiming to create the set union of the variables saved in three .mat files, then yes.
Note that you could easily test that yourself, by running this code on a few test .mat files.

Accedi per commentare.

What i got from your question you have a variable suppose x=randi(1,20); which is 1x20 in size in "file1.mat". Another variable suppose y=31; in separate mat file"file2.mat" and you ned both to be in a single mat file which could be "newfile.mat".
For this you just need to load both the files for the variables to appear in workspace and use save command to save those into new variable.
x=load('file1.mat');
y=load('file2.mat');
save('newfile','x','y')

1 Commento

Note that this will save the scalar structures x and y, which might not be the desired effect.

Accedi per commentare.

Richiesto:

il 29 Mag 2019

Modificato:

il 29 Mag 2019

Community Treasure Hunt

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

Start Hunting!

Translated by