Clear Filters
Clear Filters

how to get unique value of datetime and sum all of the value ?

26 views (last 30 days)
Hello Matlab Community
so i'm really new in Datetime class , so what i want is to sum all the value in the column 5 which is unit sold with it corrosponding dates but the dates are scatter and repeated so i want sum all of the unit with the same date , so how to do it?, here what i wrote and my brian just stop working i guess
date = FinancialSample{:,13};
U_sold = FinancialSample{:,5};
[unqdate,idx,idx1] = unique(date);
for c = FinancialSample.Date == unqdate(:)
edit: is there something not clear in my question plz ask me .

Accepted Answer

Jakob B. Nielsen
Jakob B. Nielsen on 1 Jun 2021
Hi Muhannad,
You have made a good start, you have a list of all unique dates in your unqdate. One thing I would recommend is to convert your date strings into serial date numbers, because it makes it much easier (in my opinion, anyway) to use logical indexing later on. You do this by saying
After this, you want to have a loop that executes the number of times corresponding to the size of your unqdate variable:
for i=1:size(unqdate,1)
Now you can use logical indexing to solve your problem. Its a little twisty but bear with me: you want to take from U_sold those indexes for which it is true that your full date vector matches the i'th entry in the unique date vector. And then sum the values from those indexes.
realdate{i}=datestr(unqdate(i),'dd/mm/yyyy') %and convert your unique date number back to a date string again
Logical indexing is a bit hairy when you first look at it, but think of it this way; you formulate a true/false statement, and only select the indexes for which the statement is true. An illustrative example:
A=[1 2 3 4 5 1 1];
%first, look at
%gives this: 1 0 0 0 0 1 1
%Which means if you ask for
%you get simply 1 1 1 because it only takes the values out, for which we
%got a logical 1 before.
%We are essentially doing the same thing in your problem, but for unique dates
  1 Comment
Muhannad AL-hazmi
Muhannad AL-hazmi on 1 Jun 2021
Thanke you so much
here is the full code
date = FinancialSample{:,13};
U_sold = FinancialSample{:,5};
[unqdate,idx,idx1] = unique(date);
%% this is ex for one of the date and corrosbonding Value in Unit sold .
c = FinancialSample.Date=="01-Jan-2014 00:00:00";
Z = sum(U_sold(c));
for i=1:size(unqdate1,1)
realdate{i}=datestr(unqdate1(i),'dd/mm/yyyy') %and convert your unique date number back to a date string again
hold on;
[b,indx] = max(sales);
legend('Value of Units',sprintf('Maximum Value: %d',b));
title("All of the Unit sold world wide")

Sign in to comment.

More Answers (0)


Find more on Programming 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!