Going a bit crazy here as I am not managing to use the index referencing for my newly-created timeseries objects.
All I wish to do is query/return/modify the value from a particular date.
i.e. where my timeseries "temp" is daily values for temperature and I want to change the value on January 16th 2008.
My date structure is formatted as such "01-Jan-2008"
I have tried various ways, but not managing! :(
temp('16-Jan-2008')= 25; % Info on this page ref [1]
temp(16) = 25; % 16 referring to the element index
I know that I could go into tstools and modify it manually, but I want to do much further matrix manipulation with the timeseries but am struggling somehow even with the index referencing! Can they not be modified easily in the command window?
Thanks
[1] - http://www.mathworks.co.uk/help/finance/working-with-financial-time-series-objects.html#f13-5213
For 2 vectors
time [size = n by 1] & temp [size = n by 1]
If ur timeseries is a char vector (all rows same length and not cell). U can convert to matlab time nums like
time_tmp = datenum(time,'dd-mmm-yyyy');
Then u can change a temp vector (same length as time)
temp(time_tmp == datenum(2008,1,16)) = 25;
otherwise u can convert ur data to 2 vectors then do above.
Related
I want to generate a 3D cell array called timeData so that timeData(:,:,a) for some integer a is an nx1 matrix of data, and the number of rows n varies with the value of a in a 1:1 correspondence. To do this, I am generating a 2D array of data called data that is nx1. This assignment statement takes place within a for loop as follows:
% Before iterating, I define an array of indices where I want to store the
% data sets in timeData. This choice of storage location is for
% organizational purposes.
A = [2, 5, 9, 21, 34, 100]; % Notice they are in ascending order, but have
% gaps that have no predictability.
sizeA = size(A);
numIter = A(1);
for m = 1:numIter % numIter is the number of data sets that I need to store
% in timeData
% At this point, some code that is entirely irrelevant to my question
% generates a nx1 array of data. One example of this data array is below.
data = [1.1;2.3;5.5;4.4]; % This is one example of what data could be. Its
% number of rows, n, changes each iteration, as
% do its contents.
B = size(data);
timeData(1:B(1),1,A(m)) = num2cell(data);
end
This code does put all contents of data in the appropriate locations within timeData as I want. However, it also adds {0x0 double} rows to all 2D arrays of timeData(:,:,a) for any a whose corresponding number of rows n was not the largest number of rows. Thus, there are many of these 2D arrays that have 10 to a couple hundred 0-valued rows that I don't want. For values of a that did not have a corresponding data set, the content of timeData(:,:,a) is an nx1 array of {0x0 double}.
I need to iterate over the contents of timeData in subsequent code, and I need to be able to find the size of the data set that is in timeData(:,:,a) without somehow discounting all the {0x0 double}.
How can I modify my assignment statement to fix this?
Edit: Desired output of the above example is the following with n = 5. Let this data set be represented by a = 9.
timeData(:,:,9) = {[1.1]}
{[2.3]}
{[5.5]}
{[8.6]}
{[4.4]}
Now, consider the possibility that a previous or subsequent value of the A matrix had a data set with n = 7, and n = 7 is the largest data set (largest n value). timeData(:,:,9) outputs like so in my code:
timeData(:,:,9) = {[1.1]}
{[2.3]}
{[5.5]}
{[8.6]}
{[4.4]}
{[0x0 double]}
{[0x0 double]}
#Dev-iL, as I understand it, your answer gives me the ability to delete the cells that have {[0x0 double]} in them (this is what I mean by "discounting"). This is a good plan B, but is there a way to prevent the {[0x0 double]} cells from showing up in the first place?
Edit 2: Update to the above statement "your answer gives me the ability to delete the cells that have {[0x0 double]} in them (this is what I mean by "discounting")". The cellfun(#isempty... ) function makes the {[0x0 double]}cells go to {[0x0 cell]}, it does not remove them. In other words, size(timeData(:,:,9)) is the same before and after the command is performed. This is not what I want. I want size(timeData(:,:,9)) to be 5x1 no matter what n is for any other value of a.
Edit 3: I just realized that the most desired output would be the following:
timeData(:,:,9) = {[1.1;2.3;5.5;8.6;4.4]} % An n x 1 column matrix within
% the cell.
but I can work with this outcome or the outcome as described above.
Unfortunately, I don't understand the structure of your dataset, which is why I can't suggest a better assignment method. However, I'd like to point out an operation that can you help deal with your data after it's been created:
cellfun(#isempty,timeData);
What the above does is return a logical array the size of timeData, indicating which cells contain something "empty". Typically, an array of arbitrary datatype is considered "empty" when it has at least one dimension that is equal to 0.
How can you use it to your advantage?
%% Example 1: counting non-empty cells:
nData = sum(~cellfun(#isempty,timeData(:)));
%% Example 2: assigning empty cells in place of empty double arrays:
timeData(cellfun(#isempty,timeData)) = {{}};
I have four columns of river flow data in the format (year,month,day,data). I want to check through the data to ensure that there are not any missing chunks and if there are, to create a new data array ('newdates') with the missing dates (column 1) added in, along with NaN values for the missing data (column 2).
I am checking for the missing data by subtracting the datenum of the previous day, from the datenum of the current day (if it is greater than 1, then there is data missing). My code below works, but I am omitting the last day of the data each time.
The loop I have generated to check each row is the length of the data minus 1, because if I allow it to be the length of the data, it won't be able to check the difference between the two last dates, as the date vector will not be large enough.I know this isn't an overly complex thing to do, but I can't seem to figure it out.
Any help would be appreciated. Code below:
%load a sample file of data in the format Year,Month,Date,Riverflow
>data=load('dmf_21002_279631_Q_COMPLETE_matlab.csv');
>yr=data(:,1);
>mth=data(:,2);
>day=data(:,3);
>flow=data(:,4);
>dates=datenum([yr,mth,day]);
>icounter=1;
%This is the counter for indexing the 'newdates'
>ndcounter=0
>for i =1:(length(dates)-1);
>ndcounter=ndcounter+1;
>if dates(i+1)-dates(i)==1;
>newdates(ndcounter,1)=dates(i,1);
>newdates(ndcounter,2)=data(i,4);
>elseif (dates(i+1)-dates(i))~=1;
>newdates(ndcounter,1)=dates(i,1);
> newdates(ndcounter,2)=data(i,4);
%count the number of days of data that are missing from the array
>daysmissing(icounter,1)=(dates(i+1)-dates(i))-1;
%create the missing datenums by adding ii on to the previous
%datenum (do this for the length of data missing)
>for ii=1:daysmissing;
>newdates((i+ii),1)=((newdates(i))+ii);
> newdates((i+ii),2)=NaN;
> end
>ndcounter=ndcounter+daysmissing;
>disp('Missing data found');
> missingidx(icounter)=i;
>icounter=icounter+1;
>end
>end
>newdates(newdates==-999)=NaN;
I would advise you to look into the timeseries class. On timeseries objects, you can call resample() to create missing values.
MWE:
dates = datenum([2016 2016 2016], [4 4 4], [10 11 13]);
data = [0 1 3];
ts = timeseries(data, dates);
ts2 = ts.resample(datenum([2016 2016 2016 2016], [4 4 4 4], [10:13]));
figure(1); hold on;
ts.plot('bo--');
ts2.plot('rx:');
I have an Excel sheet containing 1838 records and I need to RANDOMLY split these records into 3 Excel Sheets. I am trying to use Matlab but I am quite new to it and I have just managed the following code:
[xlsn, xlst, raw] = xlsread('data.xls');
numrows = 1838;
randindex = ceil(3*rand(numrows, 1));
raw1 = raw(:,randindex==1);
raw2 = raw(:,randindex==2);
raw3 = raw(:,randindex==3);
Your general procedure will be to read the spreadsheet into some matlab variables, operate on those matrices such that you end up with three thirds and then write each third back out.
So you've got the read covered with xlsread, that results in the two matrices xlsnum and xlstxt. I would suggest using the syntax
[~, ~, raw] = xlsread('data.xls');
In the xlsread help file (you can access this by typing doc xlsread into the command window) it says that the three output arguments hold the numeric cells, the text cells and the whole lot. This is because a matlab matrix can only hold one type of value and a spreadsheet will usually be expected to have text or numbers. The raw value will hold all of the values but in a 'cell array' instead, a different kind of matlab data type.
So then you will have a cell array valled raw. From here you want to do three things:
work out how many rows you have (I assume each record is a row) by using the size function and specifying the appropriate dimension (again check the help file to see how to do this)
create an index of random numbers between 1 and 3 inclusive, which you can use as a mask
randindex = ceil(3*rand(numrows, 1));
apply the mask to your cell array to extract the records matching each index
raw1 = raw(:,randindex==1); % do the same for the other two index values
write each cell back to a file
xlswrite('output1.xls', raw1);
You will probably have to fettle the arguments to get it to work the way you want but be sure to check the doc functionname page to get the syntax just right. Your main concern will be to get the indexing correct - matlab indexes row-first whereas spreadsheets tend to be column-first (e.g. cell A2 is column A and row 2, but matlab matrix element M(1,2) is the first row and the second column of matrix M, i.e. cell B1).
UPDATE: to split the file evenly is surprisingly more trouble: because we're using random numbers for the index it's not guaranteed to split evenly. So instead we can generate a vector of random floats and then pick out the lowest 33% of them to make index 1, the highest 33 to make index 3 and let the rest be 2.
randvec = rand(numrows, 1); % float between 0 and 1
pct33 = prctile(randvec,100/3); % value of 33rd percentile
pct67 = prctile(randvec,200/3); % value of 67th percentile
randindex = ones(numrows,1);
randindex(randvec>pct33) = 2;
randindex(randvec>pct67) = 3;
It probably still won't be absolutely even - 1838 isn't a multiple of 3. You can see how many members each group has this way
numel(find(randindex==1))
I'm new to Matlab and I'm looking for a solution to a problem of determining blocks of same dates in one vector and to average over the corresponding block of data in another vector.
Given is a vector consisting of several blocks of dates in the format 'dd-mmm-yyyy'. The blocks with same dates can have variable length. An example would be
T= ['03-Jan-2013';
'03-Jan-2013';
'03-Jan-2013';
'04-Jan-2013';
'04-Jan-2013';
'05-Jan-2013']
Each date in T corresponds to a data entry in another vector H (for simplicity same dates from T have here the same corresponding number in H)
H= [1;
1;
1;
5;
5;
6]
The goal is now to determine the average of the elements of H which correspond to the same dates and return a modified date and data vector Tout and Hout which would look like this:
Tout=['03-Jan-2013';
'04-Jan-2013';
'05-Jan-2013']
and
Hout=[1;
5;
6]
where Hout represents the averaged values.
Both vectors are initially drawn from a textfile and can have a length of about 100k.
So looping is probably not the best thing to do.
I appreciate any help!
Use unique to get the unique dates and their multiplicity and accumarray to average over the ones that are repeated
[Tout,~,n] = unique(T, 'rows');
Hout = accumarray(n, H, [], #mean);
Hi I have problem with matrix..
I have many .txt files with different number of rows but have the same number of column (1 column)
e.g. s1.txt = 1234 rows
s2.txt = 1200 rows
s2.txt = 1100 rows
I wanted to combine the three files. Since its have different rows .. when I write it to a new file I got this error = Index exceeds matrix dimensions.
How I can solved this problem? .
You can combine three matrices simply by stacking them: Assuming that s1, etc are the matrices you read in, you can make a new one like this:
snew = [s1; s2; s3];
You could also use the [] style stacking without creating the new matrix variable if you only need to do it once.
You have provided far too little information for an accurate diagnosis of your problem. Perhaps you have loaded the data from your files into variables in your workspace. Perhaps s1 has 1 column and 1234 rows, etc. Then you can concatenate the variables into one column vector like this:
totalVector = [s1; s2; s3];
and write it out to a file with a save() statement.
Does that help ?
Let me make an assumption that this question is connecting with your another question, and you want to combine those matrices by columns, leaving empty values in columns with fewer data.
In this case this code should work:
BaseFile ='s';
n=3;
A = cell(1,n);
for k=1:n
A{k} = dlmread([BaseFile num2str(k) '.txt']);
end
% create cell array with maximum number of rows and n number of columns
B = cell(max(cellfun(#numel,A)),n);
% convert each matrix in A to cell array and store in B
for k=1:n
B(1:numel(A{k}),k) = num2cell(A{k});
end
% save the data
xlswrite('output.txt',B)
The code assumes you have one column in each file, otherwise it will not work.