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 Comments

Is each cell a cell array or a char aray? In other words, dummy_list{1} is a cell array of 5 strings?
Each cell is a cell array, so dummy_list(:,1) contains 5 items (rows) of strings and its size is cell
can you convert the cell array?
That was my first attempt, try to convert the cell array. I was not able to, I tried using cell2mat function.
try to use {}, rather than () to access the content of teh cell, like dummy_list{:,k}
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
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

Sign in to comment.

 Accepted Answer

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 Comments

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}{:}
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
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.
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!

Sign in to comment.

More Answers (0)

Categories

Asked:

S
S
on 9 Aug 2012

Community Treasure Hunt

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

Start Hunting!