From: Gonzalo on
Hi there,

I need to save a cell (1000 by 6) as a .txt file. Each of the columns has strings, for example:

testcase =
'one' 'pre70' 'Wood' 'Gable' 'Shingle' 'less100k'
'one' '1971-1984' 'CB' 'Gable' 'Shingle' 'less100k'
'one' '1971-1984' 'CB' 'Gable' 'Tiles' 'less100k'

So bottom line, I need that the text file looks exactly like the cell in Matlab.
Thanks a lot
From: Walter Roberson on
Gonzalo wrote:

> I need to save a cell (1000 by 6) as a .txt file. Each of the columns
> has strings, for example:
>
> testcase = 'one' 'pre70' 'Wood' 'Gable' 'Shingle'
> 'less100k'
> 'one' '1971-1984' 'CB' 'Gable' 'Shingle' 'less100k'
> 'one' '1971-1984' 'CB' 'Gable' 'Tiles' 'less100k'
>
> So bottom line, I need that the text file looks exactly like the cell in
> Matlab.

well... okay, if that's what you want, here it goes:

outfid = fopen('OutputFile.txt', 'wt');
if outfid < 0
error('file creation failed');
end
fwrite(outfid, evalc('disp(testcase)'));
fclose(outfid);


Ummm, you do know that when a cell is displayed in Matlab, any string that is
74 characters or longer is replaced by something that looks like [1x74 char]
?? And since that is the behavior in Matlab and you say that the output in the
file has to look "exactly like the cell in Matlab", this code will replicate
that behavior and will thus not actually write out the contents of any string
74 characters or longer. But at least the text file will look *exactly* like
how Matlab would display the cell...
From: Gonzalo on
Thanks a lot Walter!

Here's another way to do it that I found.

fid = fopen('fitcaseG.txt','wt');
for i = 1 : size(fitcase,1)
fprintf(fid,'%s\t %s\t %s\t %s\t %s\t %s\t\n',fitcase{i,:});
end
fclose(fid);

THanks
 | 
Pages: 1
Prev: Ode 15s
Next: Explicit Solution Can't Be found