Use of Dates in nested structure

Hello
I have a big data that I need to classify. I would to create a structure for the string xxxx.yyyy (This is the name of entity, it has a dot). Under xxxx.yyyy, there would be a struct of dates ('2023/04/18', '2023/04/19', etc for example). Under each of these dates, there will be a range of stored variables (variable X with a column of variables, variable Y with another column of values etc). I want to emphasize on the fact that the dates would be added iteratively to the struct xxxx.yyyy (using a for loop, at i=1, the data for the date '2023/04/18' would be added and at i=2, the data for the date '2023/04/19' would be added. Also, the dates would be taken from a TABLE (table.date{1}, table.date{2} etc...)
Is this doable using Matlab?

Answers (1)

I would to create a structure for the string xxxx.yyyy (This is the name of entity, it has a dot).
By this do you mean you want to create a struct array whose name contains a dot? That is not allowed. Variable names in MATLAB must satisfy four rules:
  • Their name must start with a letter, either uppercase or lowercase.
  • Their name must contain only letters (either uppercase or lowercase), numbers, and/or the underscore character.
  • Their name must be no longer than namelengthmax characters.
  • Their name must not be a keyword.
A name like xxxx.yyyy satisfies the first, third, and fourth rules but fails the second. As such if you ask if it's a valid variable name with isvarname the answer is false:
isvarname('xxxx.yyyy')
ans = logical
0
One reason for this is that if you were allowed to have a variable named 'xxxx.yyyy' and you also had another named 'xxxx' which had a field named 'yyyy' the expression 'xxxx.yyyy' would be ambiguous.
Fieldnames of a struct array must satisfy the first three of those rules so even if you created a struct array with a valid name giving that struct a field named '05_04_2023' would not be allowed.
s = struct('05_04_2023', 42)
Error using struct
Invalid field name "05_04_2023".

8 Comments

Thanks for your reply. If I have the following data that I would prefer to arrange by date:
I understand that the name xxxx.yyyy needs to be changed and I will do that. However, what are my options if I want to store the data based on its date using a for loop (i=1, the data for the 2023/04/23 would be added to the struct xxxx_yyyy, at i=2, the data for 2023/04/24 would be added to the struct xxxx_yyyy).
"I have a big data that I need to classify."
From what I can tell, your data would probably be best stored in one table:
Thanks for your reply. I'm not sure of "table" would do what I want. what I want is to able to have one large entity called xxxx_yyyy that has subentities (date1,date2.....). Would that be possible in Matlab using tables?
"I'm not sure of "table" would do what I want."
You wrote that "I have a big data that I need to classify." Most likely "classifying" is some function of your data, which most likely could be done by performing some operations on table data:
In any case, what is your exact task? How do you need to process this data?
Note my questions are not asking about the approach you described in your question, but ask what the actual goal of this is. Presumably you have a goal that is not just "I want to store some data in structure". Tell us what that goal is.
I'd store that data as a table or timetable array instead of a struct array.
This exactly is what I want, main enitiy that has dates under it and under each date, there are buch of stored variables.
So you want to create variables whose names are only known at runtime? You can do this, but should you do this? The general consensus is no. That Answers post explains why this is generally discouraged and offers several alternative approaches.
What are you hoping to do with the data stored in some form like this? Once we know that we may be able to offer suggestions for how to most effectively store your data to facilitate your later operations on the data.
Assuming your picture is accurate, I agree with Stephen23. Create a timetable. Flat is always best if it works.
Date = datetime(2023,4,[17;17;18;18]);
Something = categorical(["yyyy_zzzz";"zzzz_aaaa";"yyyy_zzzz";"zzzz_aaaa"]);
Var1 = rand(4,1);
Var2 = rand(4,1);
tt = timetable(Date,Something,Var1,Var2)
tt = 4×3 timetable
Date Something Var1 Var2 ___________ _________ _______ _______ 17-Apr-2023 yyyy_zzzz 0.11022 0.33972 17-Apr-2023 zzzz_aaaa 0.18821 0.85904 18-Apr-2023 yyyy_zzzz 0.63592 0.99357 18-Apr-2023 zzzz_aaaa 0.22326 0.83003
If your picture isn't accurate, then you need to say why.
I also agree with Stephen23 that without knowing what you plan to do with these data, it's impossible to say what a good representation is.

Sign in to comment.

Categories

Products

Release

R2021b

Tags

Asked:

Abd
on 4 May 2023

Commented:

on 15 May 2023

Community Treasure Hunt

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

Start Hunting!