writing values from cell array to txt file

Hello,
I have a cell array let say of items containing 5 separate lists of items (so, my cell array size is 5x5, each cell containing 5 items). I want to write each list (cell) into a separate txt file.
After browsing over the forum, I came up with this code but it complains about using fprintf with cell input, giving this error: ??? Error using ==> fprintf Function is not defined for 'cell' inputs.
My code is:
for k=1:5
textFilename = ['file' num2str(k) '.txt'];
fid = fopen(textFilename, 'w');
if fid == -1
error('Cannot open file: %s', textFilename);
end
fprintf(fid,'%s',dummy_list(:,k));
end
Can someone please tell me how to accomplish this? I know the hard way is to have another for loop that goes over all the rows for each cell.
Thank you in advance.

댓글 수: 8

Oleg Komarov
Oleg Komarov 2012년 8월 9일
Is each cell a cell array or a char aray? In other words, dummy_list{1} is a cell array of 5 strings?
S
S 2012년 8월 9일
Each cell is a cell array, so dummy_list(:,1) contains 5 items (rows) of strings and its size is cell
Yash
Yash 2012년 8월 9일
can you convert the cell array?
S
S 2012년 8월 9일
That was my first attempt, try to convert the cell array. I was not able to, I tried using cell2mat function.
Feng
Feng 2012년 8월 9일
try to use {}, rather than () to access the content of teh cell, like dummy_list{:,k}
S
S 2012년 8월 9일
Using dummy_list{:,k} merges all the items in a cell at index k into a single line and saves it in the file. I want to save the items of a cell as is (one entry per line) in a txt file
Can you give an example of your dummy_list, is it something like this?
c = {{'hello' 'yes'} {'no', 'goodbye'}}
and you want to print each entry separately like
hello
yes
S
S 2012년 8월 9일
each cell is stored row-wise, so it is like: c = {{'hello' ; 'yes'} {'no' ; 'goodbye'}}
and in the first file i want to print hello yes and in the second file i want to print no goodbye

댓글을 달려면 로그인하십시오.

 채택된 답변

Honglei Chen
Honglei Chen 2012년 8월 9일

0 개 추천

Here is an example, you can insert your fid in the code and it should work
c = {{'hello' 'yes'} {'no', 'goodbye'}}
fprintf('%s %s\n',c{1}{:})

댓글 수: 14

S
S 2012년 8월 9일
Hi Honglei,
I think my previous comment did not show well, I want them to be printed in column: so if c is: c = {{'hello' ; 'yes'} {'no' ; 'goodbye'}}
it should print in one file hello yes and second file no goodbye
I think you are treating the cell arrays as stored in a single row but they are actually in a single column.
Thanks!
I am trying to have hello and yes in different lines in my post but for some reason they appear on one line (this is how they should appear in the file)
hello
yes
I don't quite follow your comment, what do you want to print in column? It's a separate file, so you need a different fid. whether c is in row or in column doesn't really matter.
In the first file, you want
hello yes
In the second file, you want
no goodbye
am I right? if that's the case, just do
fprintf(fid,'%s %s\n',c{k}{:}
S
S 2012년 8월 9일
편집: S 2012년 8월 9일
My dummy_list is (each cell is 2 by 1):
c = {{'hello' : 'yes'} {'no' : 'goodbye'}}
I want to print in the first file (similar way for second file):
hello
yes
I don't see a difference, you control the format
c = {{'hello';'yes'} {'no'; 'goodbye'}}
fprintf('%s\n%s\n',c{1}{:})
Thanks for trying to help, I modified my code but still doesnt work:
for k=1:3
textFilename = ['file' num2str(k) '.txt'];
fid = fopen(textFilename, 'w');
if fid == -1
error('Cannot open file: %s', textFilename);
end
fprintf(fid,'%s\n%s\n%s\n', dummy_list{k}{:});
end
I get this error: ??? Cell contents reference from a non-cell array object. Also, that was a dummy example, but in a real case a list can have 200 items, would that require 200 "%s\n"?
This seems to suggest either dummy_list is not a cell or dummy_list{k} is not a cell. You may need to figure that out.
Yes it will require 200 "%s\n" for 200 items, but that's not too difficult to do, you can use
fprintf(fid,repmat('%s\n',1,200),dummy_list{k}{:})
I am copying this from matlab:
>> dummy_list
dummy_list =
'A2ML1' 'A2ML1' 'AADACL3'
'A4GALT' 'A4GALT' 'AADAT'
'AACS' 'AACS' 'AAK1'
'AADACL2' 'AADACL2' 'AAMP'
'AADACL3' 'AADACL3' 'AARS'
>> class(dummy_list)
ans =
cell
>>
Then each column of your dummy_list is not a cell, it's more like this
c = {'hello' 'no'; 'yes' 'goodbye'}
fprintf('%s\n%s\n',c{:,1})
I tried that with this code but I get 3 empty files:
for k=1:3
textFilename = ['file' num2str(k) '.txt'];
fid = fopen(textFilename, 'w');
if fid == -1
error('Cannot open file: %s', textFilename);
end
fprintf(fid,'%s\n%s\n%s\n', dummy_list{:,k});
end
You should open it in the text mode and close file promptly after done. Could you try this?
for k=1:3
textFilename = ['file' num2str(k) '.txt'];
fid = fopen(textFilename, 'wt');
if fid == -1
error('Cannot open file: %s', textFilename);
end
fprintf(fid,'%s\n%s\n%s\n', dummy_list{:,k});
fclose(fid)
end
S
S 2012년 8월 9일
Very weird. Actually if I open the files using notepade I just see all the string in one line, but if I open using wordpad or excel I see them in one column as should be......I wonder!
Are you sure you used 'wt' and not 'w' ? Using 'w' instead of 'wt' can cause the behaviour you describe.
S
S 2012년 8월 9일
Ohh Thanks!!! Yes, I was using 'w'. And now I see that Honglei actually included 'wt' but when I looked at the code I overlooked it and I thought it was the same as I what I had. Thanks once again.
And Thank you very much Honglei for your time!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Data Type Identification에 대해 자세히 알아보기

질문:

S
S
2012년 8월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by