Saving to text file from cell array

1 view (last 30 days)
Ubu
Ubu on 5 Mar 2012
Dear colleagues,
we have a cell array like the one below, we obtained from a txt file:
[34] [12] 'xxx'
[3] [1] 'www'
[21] [2] 'qwe'
[11] [12] 'awk'
[34] [x] 'sed'
we just want to save it as a matrix text file with the same dimensions once again, afer having applied some replacement loops.
dlmwrite doesn't work (we're not dealing with matrixes)
save has unsupported Ascii..
fprintf ! It's the whole day we're struggling with it. Here is our code:
column1 = num2cell(colum1_from_txtfile)
column2 = num2cell(colum2_from_txtfile)
column3 = cellstr(colum3_from_txtfile)
M = [column1 column2 column3]
% our loops
fid = fopen('another_txtfile', 'wt')
fprintf(fid, '%d%d%s\t\n', M)
??? Error using ==> fprintf
Function is not defined for 'cell' inputs.
We guess the whole problem is about the value types we're using in the fprintf argument (%s %d ..).
We would be absolutely happy if anybody could provide as with suggestions!
Best,
Udiubu & Co.
  1 Comment
Walter Roberson
Walter Roberson on 5 Mar 2012
Is this a different question than your earlier http://www.mathworks.com/matlabcentral/answers/31231-txt-file-to-cell-array-to-text-file
?? If so then it would be best to indicate the difference, as otherwise someone is likely to mark it as a duplicate question that requires merging.

Sign in to comment.

Answers (2)

Walter Roberson
Walter Roberson on 5 Mar 2012
for K = 1 : size(M,1)
fprintf(fid, '%d%d%s\t\n', M{K,:});
end

Ubu
Ubu on 5 Mar 2012
Hi Walter,
Sorry to have made some caos with duplicates.
Indeed, we also thought about a loop over the first dimension, but we get some random ordering of different sizes of numbers and letters, like below:
34 12 x
120(which is 'x' in Ascii, we assume) 120 3 1
119 119 21
Thanks for your kind help.
Best,
U-

Community Treasure Hunt

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

Start Hunting!