dlmwrite/csvwrite cannot open file error

4 次查看(过去 30 天)
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!

采纳的回答

per isakson
per isakson 2014-7-21
编辑:per isakson 2014-7-24
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 个评论
Mike Beacham
Mike Beacham 2014-7-21
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!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by