convert a cell to .xls file and save it in a specified folder

does anyone know how to convert a matrix to .xls file and save it in a specified folder?
for example:
% r is a cell and path is certain
if ~exist(path\r.xls)=7
make r.xls
else
make r.Date Modified.xls in path % Date Modified can obtain by dir()
end

 Accepted Answer

data=magic(5);
file=fullfile(pathstr,'r.xls');
if exist(file,'file')
file=fullfile(pathstr,'Modified.xls');
end
xlswrite(file,data);

10 Comments

really thanks sorry i forgot to say r is cell
If r is a cell, as long as the size of every cell element is 1x1 numerical or logical data, or every cell element is string, it should work too. If any string has more than 911 characters, you may have problem. That'a a MS Office bug. Try r={1,true,'abc';'ajd',rand(1),2}.
and pleas help how can name in 'data modified' of r?
fo example r id modified in 04-SEPTEMBER-2011 18:48:28then file name must be:r_04-SEPTEMBER-2011 18:48:28.xls
So modify the example to start with:
data=cell2mat(magic(5));
Notice that Fangjun used exist(file,'file') rather than comparing exist(file) to the value 7. The value 7 is only for checking for folders (directories), but you are checking a file name, so the comparison would not work. The closest equivalent is numeric value 2, but for some file names there is a risk that you will get 3, 4, or 6 returned instead of 2. It is therefore safer to use the format Fangjun used, exist(name,'file') which covers all of the possibilities.
To include the date:
['r_' datestr(now, 'dd-mmmm-yyyy HH:MM:SS') '.xls']
If you have the data/time in string, you can do:
DateTime='04-SEPTEMBER-2011 18:48:28';
FileName=['r_',DateTime,'.xls'];
FileInfo=dir('r.xls');
FileName=['r_',FileInfo.date,'_Modified.xls'];
['r_' datestr(now, 'dd-mmmm-yyyy HH:MM:SS') '.xls']
perfect dear
but when writing .xlx errore this:
• Make sure the file name does not contain any of the following characters: < > ? [ ] : | or *
can this characters be removed from name?
It's probably complaining the whitespace between 'yyyy' and 'HH'.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!