From: jens on
Hi

I have 3 cellarrays:

column1={'1' '1' '1' '1'}% only numbers/numeric data
column2={'01-04-2009' '01-04-2009' '01-04-2009' '01-04-2009'}%date
column3={'hb' 'hb' 'much better' 'good'}% strings

I want to write the 3 cellarrays to a txt-file with 3 columns. I tried

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

I have some problems to write the second- and third column to the txt-file.

Best Regards

Jens
From: sscnekro on
> I have some problems to write the second- and third column to the txt-file.

Hey, Jens, to my impression you may need to rearrange your data prior to txt writing 'em. See what this does. It is not want you want to do, but you may extract some cell array properties from that...

column{1} = {'1' '1' '1' '1'};
column{2} = {'01-04-2009' '01-04-2009' '01-04-2009' '01-04-2009'};
column{3} = {'hb' 'hb' 'much better' 'good'}
fid = fopen('testing.txt','wt');
for ii = 1 : size(column,2)
fprintf(fid,'%s\t\n',column{ii}{1,:});
end
fclose(fid);

I fancy another problem with the fprintf() command. It writes data element by element below eo.

newcol{1} = {'1' '1' '1' '1'}';
newcol{2} = {'01-04-2009' '01-04-2009' '01-04-2009' '01-04-2009'}';
newcol{3} = {'hb' 'hb' 'much better' 'good'}';
merge = [newcol{1}, newcol{2}, newcol{3}];
fid = fopen('testing.txt','wt');
for ii = 1 : size(merge,1)
fprintf(fid,'%s\t\n',newnewcol{1,:})
end

Sorry, I am not able of helping you more, you'll need to wait for a ML Expert.
From: Matt Fig on
This utility may help you.


http://www.mathworks.com/matlabcentral/fileexchange/23840-cprintf-a-pedestrian-nd-array-formatting-and-display-utility
From: Rune Allnor on
On 5 Jun, 11:35, "jens " <pas...(a)hotmail.com> wrote:
> Hi
>
> I have 3 cellarrays:
>
> column1={'1' '1' '1' '1'}% only numbers/numeric data
> column2={'01-04-2009' '01-04-2009' '01-04-2009' '01-04-2009'}%date
> column3={'hb' 'hb' 'much better' 'good'}% strings
>
> I want to write the 3 cellarrays to a txt-file with 3 columns. I tried
>
> fid = fopen('testing.txt','wt');
>    for i = 1 : size(column1,1)
>         fprintf(fid,'%s\t\n',column1{i,:});
>    end
> fclose(fid);
>
> I have some problems to write the second- and third column to the txt-file.

You need to do something like this:

N = length(column1);
for n = 1:N % Unsafe! - need to verify that all cell
% arrays are of same length
fprintf('%g %s %s\n',column1{n},column2{n},column3{n})
end

49 01-04-2009 hb
49 01-04-2009 hb
49 01-04-2009 much better
49 01-04-2009 good

Of course, you will need to ensure that the characters
presently contained in column1 are reformatted to numbers.

Rune
From: sscnekro on
> fprintf('%g %s %s\n',column1{n},column2{n},column3{n})

Wow, I gonna learn sth today...

fid = fopen('testing.txt','wt');
for ii = 1:length(column1)
fprintf('%s\t %s\t %s\n', column1{:,ii},column2{:,ii},column3{:,ii});
end
fclose(fid);