How to copy data of one or more variables from one Mat file to another Math file?

18 views (last 30 days)
I have two Mat files namely 2sn20 and 2sn20Vectorized. They have data of different variables. Some of them are scalars and some of them are arrays. I want to copy the array "one1" of size 100x1 from 2sn20 to 2sn20Vectorized. How will we do it? Likewise if I want to copy data of two arrays from 2sn20 to 2sn20Vectorized, how will be that? And similarly, if I want to copy the data of one array and one scalar from 2sn20 to 2sn20Vectorized, how will we do that? I treid but in vain.
Regards,

Answers (2)

Walter Roberson
Walter Roberson on 11 Dec 2022
sn20_struct = load('2sn20.mat', 'one1', 'SomeArray');
one1 = sn20_struct.one1;
SomeArray = sn20_struct.SomeArray;
save('2sn20Vectorized.mat', 'one1', 'SomeArray', '-append')
This code does not care what size or datatype one1 or 'SomeArray' are, so you can use similar code for all of your variables. They key point is to load() assigning to a struct, extract the appropriate fields into the variable names you want them to appear in in the other file, and then save() using the -append option.

Stephen23
Stephen23 on 11 Dec 2022
S = load('2sn20.mat', 'one1');
save('2sn20Vectorized.mat', '-struct','S', '-append')

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!