Writing cell and numeric arrays into a same file?
Show older comments
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.
Accepted Answer
More Answers (1)
% Fake data:
X = repmat({'21-12-2016'},100,1);
Y = repmat({'Wednesday' },100,1);
Z = randi(99,100,10);
% Write data to text file:
fid = fopen('test.txt','wt');
for k = 1:numel(X)
fprintf(fid,'%s %s',X{k},Y{k})
fprintf(fid,' %u',Z(k,:))
fprintf(fid,'\n')
end
fclose(fid);
Categories
Find more on Standard File Formats 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!