Save each element from a cell as separate matrix

Lets say that we have a matrix:
tst=rand(180, 456, 10);
I would like to save each of the ten (180x456) subsets as a separate matrix automatically.
For example: x1.mat which is the tst(:,:,1);
I am doing the following:
for i=1:10
x=tst(:,:,i);
fname = sprintf('x%d.mat', i);
save(fname,'x')
end
but like this I get 10 files (x1.mat, x2.mat,...,x10.mat ) where each one contains a matrix called x.

5 comentarios

Okay and what result do you want instead?
Ioanna Thoma
Ioanna Thoma el 27 de Feb. de 2022
I want to have a folder with 10 matrices x1,x2,...x10 (without having an x matrix inside)
Catalytic
Catalytic el 27 de Feb. de 2022
I don't know why you want this, but it's almost certainly both unnecessary and a bad idea.
It is indeed possible that you might be barking up the wrong tree. It really shouldn't matter what the variable name inside the .mat file is. You can load the variable under any name you wish, e.g.,
x1=load('x1').x
What file format do you want to save the data as? For example do you want to save as xlsx? When you save as mat then there always has to be some variable name.

Iniciar sesión para comentar.

Respuestas (1)

Matt J
Matt J el 27 de Feb. de 2022
T=num2cell(tst,[1,2]);
[x1,x2,x3,x4,x5,x6,x7,x8,x9,x10]=deal(T{:});
save x x*

Categorías

Productos

Versión

R2021b

Etiquetas

Preguntada:

el 27 de Feb. de 2022

Comentada:

el 27 de Feb. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by