0

i have a cell of several matrices (all double and with the same dimension)

my_cell = 

    [172x15 double]    [172x15 double]    [172x15 double]    [172x15 double]

I would to write the matrices on txt file side by side and tabulated, to obtain a .txt file with 172 rows and 60 columns (in this case)

Divakar
  • 218,885
  • 19
  • 262
  • 358
Mixo
  • 191
  • 13

3 Answers3

2

use dlmwrite and cell2mat

mat = cell2mat(my_cell);
delimiter = ' ';  % // used to separate two values in a row in the file
filename  = 'test.txt';
dlmwrite(filename,mat,delimiter);
Nishant
  • 2,571
  • 1
  • 17
  • 29
  • ok, thank you. If I would to select only some columns of the matrices? Do you have an idea? Should I open a new topic? – Mixo May 31 '14 at 14:06
  • I tried with: mat = cell2mat(my_cell(i)); my_mat = mat(1:end, 1:12);dlmwrite(filename,mat,delimiter); but the file is overwritten by the i-mat – Mixo May 31 '14 at 19:21
  • @Mixo make a new matrix using `cat` as `new_mat = cat(2,mat(:,col_start1:col_end1),mat(:,col_start2:col_end2),.....);` and if you want to select only some columns then I believe you want to store it in a new file. But if you want to append the earlier file then use `dlmwrite('my_earlier_File.txt',new_mat,'-append',... 'delimiter',' ');` – Nishant May 31 '14 at 23:56
2
>> dlmwrite('file1.txt', [c{:}],'delimiter','\t','precision','%.5f') 

or

>> dlmwrite('file2.txt', c(:)','delimiter','\t','precision','%.5f') 

You have to choose a precision, otherwise you'll get non-uniform lines because of different numbers of decimal places.

Markus
  • 2,998
  • 1
  • 21
  • 28
1

Code

%// output_filepath is the name of your output text file

c1 = horzcat(my_cell{:})
datacell = mat2cell(c1,ones(1,size(c1,1)),ones(1,size(c1,2)))
dlmwrite(output_filepath,datacell,'\t'); %// a TAB delimiter is used
Divakar
  • 218,885
  • 19
  • 262
  • 358