MATLAB: Writing cell and numeric arrays into a same file

csvwritedlmwritefile writetext filewriting arrays to file

Hi,
I am struggling to find a way to write a cell string array and a numeric matrix to the same output file. Here is a quick snapshot of the problem I am facing.
I have the following three columns:
X is a column vector of size (100,1) which has the same date value '21-12-2016' across the 100 rows
Y is a column vector of size (100,1) which has the same day value 'Wednesday' across the 100 rows
Z is a matrix of size (100,10) which has different set of 10 numbers (example [1 2 3 4 5 6 7 8 9 10]) on each of the 100 rows
Now, I wish to write these three arrays into the same file.
I tried options like csvwrite and dlmwrite but they aren't helping much.
The kind of output I get when I use either of these two commands looks like this:
2,1,-,1,2,-,2,0,1,6,W,e,d,n,e,s,d,a,y,1,2,3,4,5,6,7,8,9,10
As you can see, each bit is now separated by a comma…
This is what is confusing. How did the "," creep between 2 and 1 in the date 21 for example?
Instead, what I want is like this:
21-12-2016 Wednesday 1 1 2 3 4 5 6 7 8 9 10
I would really appreciate any kind of advice or a solution method.
Thanks in advance.

Best Answer

N = 10 ;
A = rand(N,10) ;
d = date ;
day = 'Tuesday' ;
f1 = '%s %s' ;
f2 = [repmat(' %f ', 1, 10) '\n'] ;
f = strcat(f1,f2) ;
fid = fopen('iwant.txt','w') ;
for i = 1:N
fprintf(fid,f,d,day,A(i,:)') ;
end
fclose(fid) ;