How to write cell array of various data types to a text file using fprintf
Show older comments
I am working with a number of different data types, and I want to print them all to the same text file. They are currently stored in a cell array of cell, int32, and double formats (see example format below for output_data). Each cell is the same size, so there should be no issues there. However, I can not find a way to actually print this to a file. If I do the routine below, I get the following error: Function is not defined for 'cell' inputs. Do you have any ideas as to how I could fix this or work around it? I have seen it suggested to write the data to the file line by line, but because of the differing data types, I am not able to access each line of each cell on each iteration.
output_data =
<2316x1 cell>
<2316x1 int32>
<2316x1 cell>
<2316x1 int32>
<2316x1 double>
<2316x1 int32>
output_format = [ repmat('%s\t ',1,1) repmat('%d\t ',1,1) repmat('%s\t ',1,1) repmat('%d\t ',1,1) repmat('%f\t ',1,1) repmat('%d\t ',1,1) ];
fprintf(fid,output_format,output_data)
6 Comments
Walter Roberson
on 20 Aug 2013
Is everything to go onto a single line? In which order? All of output_data{1} followed (on the same line) by all of output_data{2}, and so on?
Karen
on 20 Aug 2013
Walter Roberson
on 20 Aug 2013
What format would you expect for columns that are themselves type cell ?
dpb
on 20 Aug 2013
The int32 and double are not problem; what's in the cells? You'll have to dereference them at the very least, obviously, but what you can/should do w/ them is totally dependent on their content.
Karen
on 20 Aug 2013
Walter Roberson
on 20 Aug 2013
"generally" you say. That implies that some do not? For any one output_data{K} are all the entries the same data type?
Are the strings fixed-length? Could they have blanks inside them? Could they have commas inside them?
Are you wanting to use commas to separate the fields, or use spaces, or something else?
When you say they generally have strings, is that a cell array of strings, or is that a single string that for some reason has been wrapped inside of a cell instead of existing just as a char vector ?
Answers (1)
OK, on the assumption your cell contents are something like
>> c={'abc';'b'}; o={c; [2;23]}
o =
{2x1 cell }
[2x1 double]
>>
You'll have to dereference the cell contents and convert to char for the write--
>> fmt='%s\t %d\t\n'; % there's no point in the repmat() you used...
>> for i=1:length(o{1})
l=sprintf(fmt,o{1}{i},o{2}(i))
end
l =
abc 2
l =
b 23
As Walter pointed out, NB the {} (curlies) on the second subscript for the cellstr content vis a vis the () (normal parens) for the numeric cell. They accomplish the same as did the char() previously using the (). Also note his earlier comments/question re: things like embedded spaces, etc., etc., that may mean need to add quoting and/or other niceties.
2 Comments
Walter Roberson
on 21 Aug 2013
You do not need the char(). Use o{1}{i}
Ah! That's so...edit'ed to reflect. I tend to forget that can use the curlies as second in conjunction w/ first set 'cuz I've done the wrong-way 'round enough times getting the subsequent 'Error: ()-indexing must appear last in an index expression. message that end up using the plain paren's just automatically.
Categories
Find more on Cell Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!