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

21 次查看(过去 30 天)
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,

回答(2 个)

Walter Roberson
Walter Roberson 2022-12-11
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 2022-12-11
S = load('2sn20.mat', 'one1');
save('2sn20Vectorized.mat', '-struct','S', '-append')

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by