writing data to delimeted file with column headers and numeric data

9 views (last 30 days)
Hi,
I'm trying to write data and string column headers to a delimited file in MATLAB7.11 on CentOS and havig trouble. I have data on a number of subjects, and two data points per subject. Right now my subject names are stored in a cell array that looks like: {'sub1', 'sub2', .....}, and the data for each subject are stored in a matrix that looks like: [sub1val1, sub2val2; sub1val2, sub2val2 .....].
I want to eventually export this data to excel where each subject's data are stored in columns, and the subject's name is the column header i.e. the first row is all subject names. I'm currently using fprintf to print all of the subject names in the first row, then attempting to use dlmwrite with '-append' to append the same file with the data stored in the matrix. My code looks like this:
fid=fopen('test_file.dat');
for i=1:length(subjects)
fprintf(fid,'%s',subjects{i});
end
fclose(fid);
dlmwrite('test_file.dat', M,'-append','delimiter',',');
It does't seem like the fprintf step is working. When I run this step through the command line manually, repeating for a couple subjects it seems to work fine, however when I run it in my script it doesnt work. I dont think that dlmwrite is writing over the file because I've troubleshooted it by putting keyboard in before the dlmwrite step and checking test_file.dat, which is empty at that stage.
Can anyone help me figure out what's going on, or sugest a better way of writing my data in the desired form. I eventually want to get it into excel so I can manipulate the data there, and I can't use xlswrite because I'm on a linux installation without Microsoft Office. Any advice would be much appreciated.
Also, I would like to be able to write a series of such comma/tab delimited files with the same set of subjects i.e. column headers, but with a couple different sets of data for each subject, whih is why I intially stored my data like I did. If there is a way for me to export cell array data for excel or a structure to excel I could store my data one of those ways and then export.
-J
  1 Comment
Jeremy Jacobsen
Jeremy Jacobsen on 1 Aug 2016
I'm looking for an answer to this as well. It baffles me that someone at mathworks hasn't written a general module to handle file I/O. This sort of thing puts Matlab a decade behind Python and R where this sort of thing is a one liner. Sad really.

Sign in to comment.

Answers (2)

Azzi Abdelmalek
Azzi Abdelmalek on 12 Feb 2014
Edited: Azzi Abdelmalek on 12 Feb 2014
subjects={'sub1', 'sub2', 'sub3'}
M=rand(5,3)
v=[subjects;num2cell(M)]
xlswrite('test_file.xlsx',v)
% Or you can use fprintf with text files
subjects={'sub1', 'sub2', 'sub3'}
M=rand(5,3)
fid=fopen('test_file.txt','w');
fprintf(fid,' %s %s %s \r\n',subjects{:});
for k=1:size(M,1)
fprintf(fid, ' %5.4f',M(k,:))
fprintf(fid, '\r\n')
end
  1 Comment
Jared Z
Jared Z on 13 Feb 2014
Hi Azzi,
Thanks a lot for your help. I can't use the first suggestion because I'm on a Linux installation and don't have MIcrosoft Office. Also, can I use fprintf to print the string data and then use dlmwrite to write the numerical data?
Thanks, J

Sign in to comment.


Walter Roberson
Walter Roberson on 1 Aug 2016

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!