Borrar filtros
Borrar filtros

Split 3d array into 'i' equal mat files

2 visualizaciones (últimos 30 días)
Kash022
Kash022 el 21 de Mzo. de 2016
Comentada: Star Strider el 22 de Mzo. de 2016
Hello All,
I have a matrix (called my_matrix) which is of size() 256 x 6 x 2000. Could you please let me know how to split this array into six separate .mat files (or some other easier way) each having 256 x 1 x 2000? I tried using for loop as below but it didn't work out.
for i = 1:6
filename[i] = 'my_data[i].mat';
save(filename[i],'my_matrix(:,i,:)';'-mat');
end
Thanks! \ksnf3000

Respuesta aceptada

Star Strider
Star Strider el 21 de Mzo. de 2016
See if this does what you want:
M = randi(99, 10,6, 15); % Create Matrix
C = num2cell(M, [1 3]); % Cell Array
for k1 = 1:size(C,2)
filename = sprintf('my_data%d.mat', k1);
my_matrix = squeeze(C{k1})'
save(filename, 'my_matrix')
end
NOTE I tested everything except the save call because I didn’t want to have to go back and delete those files. It should work as written.
  2 comentarios
Kash022
Kash022 el 22 de Mzo. de 2016
Hi, Thank you. It works. Is there a way that I can extract data such as min(),max(),average() from all the 6 instances? If these are say, part of monte carlo simulations then I would like to extract the above parameters across 6 simulations. I tried the following code but it does not help.
h=gcf;
axesObjs = get(h, 'Children'); %axes handles
dataObjs = get(axesObjs, 'Children');
objTypes = get(dataObjs, 'Type');
xdata=get(dataObjs,'XData');
ydata=get(dataObjs,'YData');
y_max =max(ydata);
Thanks!
Star Strider
Star Strider el 22 de Mzo. de 2016
My pleasure.
I don’t know where the graphics references are coming from. If you’re getting information from a .fig file or a plot, that’s a separate issue and depends on the version of MATLAB you’re using, since this changed significantly beginning with R2014b.
With just the matrix and the cells created from it, this is how I would do it:
M = randi(99, 10,6, 15); % Create Matrix
C = num2cell(M, [1 3]); % Cell Array
Cellmax = cellfun(@(x) max(x(:)), C, 'Uni',0); % Maximum
Cellmin = cellfun(@(x) min(x(:)), C, 'Uni',0); % Minimum
Cellmean = cellfun(@(x) mean(x(:)), C, 'Uni',0); % Mean (Average)
These produce cell arrays as output. To convert them to numeric arrays, use the cell2mat function, for example:
MaxMtx = cell2mat(Cellmax);
and so for the others.

Iniciar sesión para comentar.

Más respuestas (1)

Azzi Abdelmalek
Azzi Abdelmalek el 21 de Mzo. de 2016
Editada: Azzi Abdelmalek el 21 de Mzo. de 2016
v=rand(4,6,8)
for ii= 1:6
filename=sprintf('my_data%d.mat',ii);
s=v(:,ii,:)
save(filename,'s');
end

Community Treasure Hunt

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

Start Hunting!

Translated by