Effacer les filtres
Effacer les filtres

Use of Dates in nested structure

2 vues (au cours des 30 derniers jours)
Abd
Abd le 4 Mai 2023
Commenté : Peter Perkins le 15 Mai 2023

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?

Réponses (1)

Steven Lord
Steven Lord le 5 Mai 2023
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 commentaires
Steven Lord
Steven Lord le 5 Mai 2023
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.
Peter Perkins
Peter Perkins le 15 Mai 2023
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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Tags

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by