Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Dóra Kocsis
dynare
Commits
93e6b41d
Commit
93e6b41d
authored
Nov 19, 2012
by
Stéphane Adjemian
Browse files
Added routines that will be used to import data (csv file) in a dynSeries object.
parent
c3ff5d92
Changes
8
Hide whitespace changes
Inline
Sidebyside
matlab/@dynSeries/private/check_file_extension.m
0 → 100644
View file @
93e6b41d
function
b
=
check_file_extension
(
file
,
type
)
% Copyright (C) 2012 Dynare Team
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
% AUTHOR(S) stephane DOT adjemian AT univ DASH lemans DOT fr
remain
=
file
;
while
~
isempty
(
remain
)
[
ext
,
remain
]
=
strtok
(
remain
,
'.'
);
end
b
=
strcmp
(
ext
,
type
);
\ No newline at end of file
matlab/@dynSeries/private/get_cells_id.m
0 → 100644
View file @
93e6b41d
function
[
B
,
C
]
=
get_cells_id
(
str
,
sep
)
% Copyright (C) 2012 Dynare Team
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
% AUTHOR(S) stephane DOT adjemian AT univ DASH lemans DOT fr
sep_locations
=
transpose
(
strfind
(
str
,
sep
));
B
=
[
1
;
sep_locations
+
1
];
C
=
[
sep_locations

1
;
length
(
str
)];
\ No newline at end of file
matlab/@dynSeries/private/isint.m
0 → 100644
View file @
93e6b41d
function
b
=
isint
(
a
)
% Copyright (C) 2012 Dynare Team
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
% AUTHOR(S) stephane DOT adjemian AT univ DASH lemans DOT fr
b
=
(
floor
(
a
)
==
a
);
\ No newline at end of file
matlab/@dynSeries/private/ismonthly.m
0 → 100644
View file @
93e6b41d
function
b
=
ismonthly
(
date
)
% Copyright (C) 2012 Dynare Team
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
% AUTHOR(S) stephane DOT adjemian AT univ DASH lemans DOT fr
[
year
,
remain
]
=
strtok
(
date
,
'M'
);
if
~
isint
(
str2num
(
year
))
b
=
0
;
return
end
[
month
,
remain
]
=
strtok
(
remain
,
'M'
);
if
~
isempty
(
remain
)
b
=
0
;
return
end
month
=
str2num
(
month
);
if
~
isint
(
month
)

month
<
1

month
>
12
b
=
0
;
return
end
b
=
1
;
\ No newline at end of file
matlab/@dynSeries/private/isquaterly.m
0 → 100644
View file @
93e6b41d
function
b
=
isquaterly
(
date
)
% Copyright (C) 2012 Dynare Team
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
% AUTHOR(S) stephane DOT adjemian AT univ DASH lemans DOT fr
[
year
,
remain
]
=
strtok
(
date
,
'Q'
);
if
~
isint
(
str2num
(
year
))
b
=
0
;
return
end
[
quarter
,
remain
]
=
strtok
(
remain
,
'Q'
);
if
~
isempty
(
remain
)
b
=
0
;
return
end
quarter
=
str2num
(
quarter
);
if
~
isint
(
quarter
)

quarter
<
1

quarter
>
4
b
=
0
;
return
end
b
=
1
;
\ No newline at end of file
matlab/@dynSeries/private/isweekly.m
0 → 100644
View file @
93e6b41d
function
b
=
isweekly
(
date
)
% Copyright (C) 2012 Dynare Team
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
% AUTHOR(S) stephane DOT adjemian AT univ DASH lemans DOT fr
[
year
,
remain
]
=
strtok
(
date
,
'W'
);
if
~
isint
(
str2num
(
year
))
b
=
0
;
return
end
[
week
,
remain
]
=
strtok
(
remain
,
'W'
);
if
~
isempty
(
remain
)
b
=
0
;
return
end
week
=
str2num
(
week
);
if
~
isint
(
week
)

week
<
1

week
>
52
b
=
0
;
return
end
b
=
1
;
\ No newline at end of file
matlab/@dynSeries/private/isyearly.m
0 → 100644
View file @
93e6b41d
function
b
=
isyearly
(
date
)
% Copyright (C) 2012 Dynare Team
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
% AUTHOR(S) stephane DOT adjemian AT univ DASH lemans DOT fr
year
=
str2num
(
date
);
if
~
isempty
(
year
)
if
isint
(
year
)
b
=
1
;
else
b
=
0
;
end
return
else
[
year
,
remain
]
=
strtok
(
date
,
'Y'
);
if
~
isequal
(
remain
,
'Y'
)
b
=
0
;
return
end
if
~
isint
(
str2num
(
year
))
b
=
0
;
return
end
b
=
2
;
end
\ No newline at end of file
matlab/@dynSeries/private/readcsv.m
0 → 100644
View file @
93e6b41d
function
[
list_of_variables
,
data
,
time
]
=
readcsv
(
file
,
withtime
,
withnames
,
noemptycell
)
% Copyright (C) 2012 Dynare Team
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
% AUTHOR(S) stephane DOT adjemian AT univ DASH lemans DOT fr
if
~
withtime
&&
~
withname
&&
noemptycell
% Use matlab builtin routine!
data
=
csvread
(
file
);
end
if
~
(
isequal
(
withtime
,
0
)

isequal
(
withtime
,
1
)
)
error
(
'readcsv:: Second input argument has to be equal to 1 or 0!'
)
end
if
~
(
isequal
(
withnames
,
0
)

isequal
(
withnames
,
1
)
)
error
(
'readcsv:: Third input argument has to be equal to 1 or 0!'
)
end
% Output initialization
time
=
[];
data
=
[];
list_of_variables
=
[];
% Check if file exists.
if
check_file_extension
(
file
,
'csv'
)
try
fid
=
fopen
(
file
,
'r'
);
catch
error
([
'readcsv: I can
''
t find file '
file
'!'
])
end
else
error
(
'readcsv: Wrong file extension!'
)
end
% bfile contains a vector of ascii codes.
bfile
=
fread
(
fid
);
% Close (csv) file.
fclose
(
fid
);
% Set newline code (ok for *nix, check for mac and windows)
if
isunix
newline_code
=
10
;
else
error
(
'readcsv:: Not implemented for your OS!'
)
end
% Get the positions of the endofline code;
end_of_line_locations
=
find
(
bfile
==
newline_code
);
tmp
=
find
(
bfile
==
newline_code
);
% Get the number of lines in the file.
ndx
=
length
(
tmp
);
% Create a cell of indices for each line.
b
=
[
1
;
end_of_line_locations
+
1
];
c
=
[
end_of_line_locations

1
;
length
(
bfile
)
+
1
];
b
=
b
(
1
:
end

1
);
c
=
c
(
1
:
end

1
);
linea
=
1
;
if
withnames
% Get the first line of the csv file (names of the variables).
linee
=
char
(
transpose
(
bfile
(
b
(
linea
):
c
(
linea
))));
% Get the content of the first line and determine the number of variables and their names.
[
B
,
C
]
=
get_cells_id
(
linee
,
','
);
if
withtime
B
=
B
(
2
:
end
);
C
=
C
(
2
:
end
);
end
list_of_variables
=
cell
(
length
(
B
),
1
);
number_of_variables
=
length
(
list_of_variables
);
for
i
=
1
:
number_of_variables
list_of_variables
(
i
)
=
{
linee
(
B
(
i
):
C
(
i
))};
end
linea
=
linea
+
1
;
end
% Get following line (number 1 or 2 depending on withnames flag)
linee
=
char
(
transpose
(
bfile
(
b
(
linea
):
c
(
linea
))));
comma_locations
=
transpose
(
strfind
(
linee
,
','
));
B
=
1
;
C
=
comma_locations
(
1
)

1
;
if
withtime
tmp
=
linee
(
B
:
C
);
% Check the dates formatting
if
~
(
isyearly
(
tmp
)

isquaterly
(
tmp
)

ismonthly
(
tmp
)

isweekly
(
tmp
))
error
(
'readcsv:: Formatting error. I can
''
t read the dates!'
)
end
if
isyearly
(
tmp
)
==
2
% Remove the Y (gpm/iris date format) if necessary
tmp
=
{
tmp
(
1
:
end

1
)
};
end
initial_date
=
dynDate
(
tmp
);
first
=
2
;
else
initial_date
=
dynDate
(
1
);
first
=
1
;
end
if
~
withnames
number_of_variables
=
length
(
tmp
)

withtime
;
end
% Initialization of matrix data.
data
=
zeros
(
ndx
,
number_of_variables
);
% Populate data.
for
linea
=
1
+
withnames
:
ndx
linee
=
char
(
transpose
(
bfile
(
b
(
linea
):
c
(
linea
))));
[
B
,
C
]
=
get_cells_id
(
linee
,
','
);
for
i
=
first
:
length
(
B
)
if
isequal
(
B
(
i
),
C
(
i
))
data
(
linea
,
i

withtime
)
=
NaN
;
else
data
(
linea
,
i

withtime
)
=
str2double
(
linee
(
B
(
i
):
C
(
i
)));
end
end
end
\ No newline at end of file
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment