How do I export a cell array containing double arrays into an ASCII-file format?

조회 수: 25(최근 30일)
I have a cell array containing double arrays of one row and different length of columns. I want to export it into a file.

채택된 답변

MathWorks Support Team
MathWorks Support Team 2021년 10월 12일
편집: MathWorks Support Team 2021년 10월 12일
The SAVE function will only save cell arrays to a MAT-file. To save your cell array to an ASCII-file, you will need to use the FPRINTF function to save cell arrays to an ASCII-file. The following example demonstrates how to save a cell array to an ASCII-file using the FPRINTF function:
x={[1 2] [2 3 4] [5 6 7 8]};
fid = fopen('cell_array.txt','w');
for i=1:length(x)
fprintf(fid,'%d ',x{i});
fprintf(fid,'\n');
end
fclose(fid);
type cell_array.txt
If you have a cell array which contains characters (or other non-numeric data) you will need to modify the above code to properly format your data in the text file. More information is available by executing the following command in MATLAB
doc fprintf
or in the following tech note on our website:
You can export a cell array containing double array of one row and different lengths of columns using the SAVE function in a MAT-file. The following is a sample code that you can use to achieve this:
x={[1 2] [2 3 4] [5 6 7 8]};
save cell_array % this will save the cell array x in file cell_array.mat
You can then import this data in MATLAB workspace using the LOAD function:
load cell_array
For more information about the SAVE and LOAD functions, refer to the MATLAB documentation using either "help save" or "doc save" (same for LOAD) commands at the MATLAB Command Prompt.

추가 답변(1개)

Thumree Sarkar
Thumree Sarkar 2017년 11월 9일
편집: Thumree Sarkar 2017년 11월 9일
I wanted to ask a question related to this post. I have a 6X2 cell, where the first columns are six 'strings' and in second column some numbers. The 6X2 cell is like
'NCOLS' 145
'NROWS' 73
'XLLCORNER' -90
'YLLCORNER' -180
'CELLSIZE' 2.5
'NODATA_VALUE' -9999
I want to append a 73x145 matrix below this two column cell and save in an .asc file. I have tried to use dlmwrite, but cell could not be written using dlmwrite and the cell could not be saved in a matrix array. I am using Matlab 2016a.

Community Treasure Hunt

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

Start Hunting!

Translated by