Copying data from a .dat file to a .xls file

9 views (last 30 days)
Frances
Frances on 27 Aug 2012
I have a lot of .dat files, each with a bunch of text and then some numbers that I want to copy into a single excel spreadsheet. Here is what I have done so far. First, I read the .dat file into a string:
dattext = fileread(graph);
I then found the data I wanted using a regular expression:
rowcolexp = '#\srows:\s\S*?#\scolumns:\s\S*?\s';
matchend = regexp(dattext, rowcolexp, 'once', 'end');
graphdata = dattext(matchend+1:end);
This data is now in a form like '-30.2001 0 2.34 45.2223 15.071' (a string). Next I tried textscan to break up the numbers, which are delimited by spaces, and then reshaping it:
nums = textscan(graphdata, '%s');
nums = reshape(nums{1}, cols, rows)';
This gives me a cell array containing the numbers in the right places. At this point, I tried to use xlswrite to write nums to an xls file, but this function, dlmwrite, and csvwrite all gave me an error saying "The input cell array cannot be converted to a matrix." When I tried to convert it to a matrix myself using cell2mat, it said "??? Error using ==> cat. CAT arguments dimensions are not consistent." Why can't I make my cell array into a matrix?
If I use char, it puts each number on its own line, but doesn't recognize it as all one number - I would have to do some other steps to retrieve each number.
On the other hand, if I open the .dat file in an excel spreadsheet myself (without editing it at all, so still containing the text) and select "space delimiters", it opens with all the right columns and rows. This must mean there are newline characters in it, which for some reason don't have any effect when I open it in Notepad. Is there some way for me just to copy the data from the .dat file and into the .xls without getting rid of these newlines? This would be an even better solution.
EDIT:
I've found that this works with xlswrite, but not with csvwrite or dlmwrite. Here is the example that I was going to post:
After finding it in the string from the original file, this is what the data looks like:
graphdata = '-30.0062 0.29949 146.9117 5.0110 0 5.2058 14.2340 1.902 89.234';
After using textscan, it becomes a cell array:
nums = textscan(graphdata, '%s')
nums =
{9x1 cell}
You then have to access the first cell to see all the numbers:
nums{1} =
'-30.0062'
'0.29949'
'146.9117'
'5.0110'
'0'
'5.2058'
'14.2340'
'1.902'
'89.234'
You can then reshape this to have the right number of columns and rows:
nums = reshape(nums{1}, cols, rows)'
nums =
'-30.0062' '5.0110' '14.2340'
'0.29949' '0' '1.902'
'146.9117' '5.2058' '89.234'
At this point, if I do either of the following:
csvwrite('testfile.txt', nums);
dlmwrite('testfile.txt', nums);
I get an error that says "The input cell array cannot be converted to a matrix". However, xlswrite worked fine! I can use xlswrite for my problem, but I'm just wondering for future reference why this didn't work with dlmwrite or csvwrite.
  2 Comments
Oleg Komarov
Oleg Komarov on 27 Aug 2012
It's hard to imagine what nums is. Can you post a small example that gives you that error?
Frances
Frances on 28 Aug 2012
See the edit I've just added to my question.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!