# Splitting a Matrix and saving results

3 Ansichten (letzte 30 Tage)
Robert am 18 Jun. 2015
Kommentiert: James Tursa am 18 Jun. 2015
I have a matrix (35x5461). I need to split it into 127 matrices of (35x43)
Then each matrix needs to be saved as an ascii with a given prefix, followed by the matrix number
eg data0001.txt, data0002.txt, data0003.txt etc
I've tried using cell2mat with no luck, and am totally stuck on the outputs, any advice would be greatly appreciated
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Antworten (1)

James Tursa am 18 Jun. 2015
Bearbeitet: James Tursa am 18 Jun. 2015
You can use a reshape to help isolate your matrices.
x = reshape(your_matrix,35,43,127);
Then x(:,:,k) is the k'th individual matrix.
The filename can be created with:
fname = sprintf('data%04d.txt',k);
But do you really need the data spit in separate files like this? It will be a pain to read them all in and combine them into a single matrix later on. Can't you just save the reshaped matrix, and then read in the whole thing and pick off your x(:,:,k) part for processing? What program is reading this data downstream?
##### 2 KommentareKeine anzeigenKeine ausblenden
Robert am 18 Jun. 2015
Thanks for your answer, Unfortunately another Code I am going to use this data in is written such that it loads all the .txt files in a directory to read, rather than splitting up one file (I'm not matlab literate enough to alter that just yet).
In the answer you gave above, how does the 'k' translate into the actual code? I get what k is representing, the 3rd dimensio, but If I were to type the bottom part of the code in I'm guessing it would say 'k' is undefined...
James Tursa am 18 Jun. 2015
E.g.,
x = reshape(your_matrix,35,43,127);
for k=1:127
individual_matrix = x(:,:,k);
fname = sprintf('data%04d.txt',k);
% write the variable individual_matrix to the file fname here
end

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Logical finden Sie in Help Center und File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by