Organizing dates and values using simple matrices

I have data of that look as that looks like this:
Year Month Day1 Day2 Day3 ... Day31
1999 01 VAL VAL VAL VAL
1999 02 VAL VAL VAL VAL
It's a csv file and VAL is a real number. I'm using a mix of textread and csvread to open them (depending, some of the files have headers that need to be included) and it seems like a lot of matrix rearranging. I think I have an idea of how to use MATLAB to organize them into cell arrays, but actually cell arrays won't help me in the end. What is needed is just a x-by-2 matrix that basically is like
1999-01-01 VALUE
1999-01-02 VALUE
1999-01-03 VALUE
1999-01-04 VALUE
I'm mainly not sure if the first column is possible like that or if it needs to be either a string or a x-by-4 matrix like this
1999 01 03 VAL
Thanks for any suggestions.

2 Commenti

Take a look at structures. They might solve your problem.
Sorry, I mentioned cell arrays wouldn't work in the end, and the same is for structures. It has to be a matrix.

Accedi per commentare.

Risposte (1)

Azzi Abdelmalek
Azzi Abdelmalek il 25 Lug 2015
Modificato: Azzi Abdelmalek il 26 Lug 2015
Edit
%-----------------Example------------------------
A=[ {'year' 'month'} genvarname(repmat({'day'},1,31),'day');repmat({1999},12,1) num2cell((1:12)') num2cell(rand(12,31))]
%----------------The code------------------------
B=A(2:end,3:end);
BB=B(:);
y=repmat(A(2:end,1),1,31)';
y=y(:);
m=repmat(A(2:end,2),1,31)';
m=m(:);
d=num2cell(repmat((1:31)',size(B,1),1));
out=[y m d BB];
idx=cellfun(@isempty,out(:,4)); %-----------Edited line--------------
out(idx,:)=[]

4 Commenti

I'm just giving this a brief try, I'm getting an error with the idx and cell 'isempty' line, "Conversion to double from function_handle is not possible". Using 2013, but I have access to 2014 I think, if that makes a difference. I'll check in depth in a day to see if this works better then. Thanks a bunch.
idx = cellfun(@isempty,out(:,4));
Okay so this is what I want I think. However, the day values don't quite match up correctly with their respective dates. For example, just compare January 1st and 2nd 1999 before and after. I've been trying to fix this myself with matrix manipulation but with no luck. Any advice? I tried some transposes also that I thought might work, but those ended up getting me errors with the concatenations.
Okay, I've figured out the issue on my own and can output to a file. My question now is: is there anyway for the dates to by hyphenated or stored in two matrices upon saving to a file or something for use in a database?

Accedi per commentare.

Prodotti

Richiesto:

il 25 Lug 2015

Commentato:

il 27 Lug 2015

Community Treasure Hunt

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

Start Hunting!

Translated by