dlmwrite/csvwrite cannot open file error

4 views (last 30 days)
Hi, I'm trying to write a script that saves a new file for each day in data taken over the course of a year. I think I have everything right, but I keep getting an error that says I can't open the file. In my mind, it's not supposed to be opening the file, it's supposed to be writing it, but anyway, any help as to where I went wront would be wonderful. Code is included below, then the error message I got.
function [Filetosave]=splitdate(InputFile)
%This function will take a given file and split the data apart by day for
%to limit file sizes. It should recognize Dates of string, number or
%vector format, so long as vector format is |Y|M|D|H|M|S|.
Date=InputFile(:,1);
if isnumeric(Date)
%date is datenum
check='num';
if Date<600000
MatTime = (Date + (693960));
else
MatTime=Date;
end
elseif iscell(Date)
%date is datevec
MatTime=datenum(Date);
else
%date is datestr
check='string';
MatTime=datenum(Date);
end
[yr,mo,day,hr,min,sec]=datevec(MatTime);
for n=(1:numel(Date))
if (n)==1
Filetosave=InputFile(1,:);
elseif day(n)==day(n-1)
Filetosave = vertcat(Filetosave(:,:),InputFile(n,:));
else
nameitthis=sprintf('Date_%d/%d/%d.csv',mo(n-1),day(n-1),yr(n-1));
dlmwrite(sprintf('%s',nameitthis),Filetosave);
Filetosave=InputFile(n,:);
end
end
end
Error message looks like this, and if I used csvwrite, it gave the same thing, but with a error in csvwrite calling the dlmwrite function.
Error using dlmwrite (line 124)
Could not open file Date_1/4/2011.csv
Error in splitdate (line 32)
dlmwrite(sprintf('%s',nameitthis),Filetosave);
Thanks in advance!

Accepted Answer

per isakson
per isakson on 21 Jul 2014
Edited: per isakson on 24 Jul 2014
dlmwrite is a function written in m-code. Every time you run dlmwrite it opens and closes the file.
  • 'Date_1/4/2011.csv' is not a legal file name. Or the folders 'Date_1' and '4' do not exist.
  • dlmwrite(sprintf('%s',nameitthis),Filetosave); is a bit overdoing: dlmwrite(nameitthis,Filetosave);
  1 Comment
Mike Beacham
Mike Beacham on 21 Jul 2014
I didn't even think about the slashes being folders. Whoops. Changed to Date_1-4-2011 and more or less happy, except it only exports one file. I'll keep working. Thank you!

Sign in to comment.

More Answers (0)

Categories

Find more on Dates and Time 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!