Name Structure in a for loop

10 vues (au cours des 30 derniers jours)
Ernest van Niekerk
Ernest van Niekerk le 13 Août 2019
I have a data analysis assignment that requires me to process 7311 sets of accumilated route data. Every set belongs to one of 21 routes named route1-route21. I have one struct with all the data but now I have to create a structure for every individual route containing only the sets of data relevant to that route. I am trying to do this in a for loop as follows:
count = 1;
for i = 1:RouteName_catagories_count
for j = 1:size(data_struct_sorted_unique.RouteName(:),1)
if data_struct_sorted_unique.RouteName(j,1) == RouteName_catagories(i,1)
for k = 1:num_fields
name = RouteName_catagories(i,1)
name.(field_names(k,1))(count,1) = data_struct_sorted_unique.(field_names(k,1))(count,1);
end
count=count+1;
end
end
end
But then I get the error: "Unable to perform assignment because dot indexing is not supported for variables of this type." I would not like to hardcode this because the next set of questions requires me to create structures for every driver and there are 300 of them. So hardcoding each one is impractical. I know dynamically naming variables is bad practice. This is specifically ask in the assignment: "Create different data structures for each RouteName. For each RouteName, create different data structures for each DriverID (i.e. for each RouteName-DriverID combination)."
Im using matlab 2017b
Any help or advice is greatly appreciated thank you.
  1 commentaire
Stephen23
Stephen23 le 13 Août 2019
Modifié(e) : Stephen23 le 13 Août 2019
"Every set belongs to one of 21 routes named route1-route21"
So the names neatly correspond to indexing, which makes your task easy.
"So hardcoding each one is impractical."
Yes, it sure would be! Luckily indexing is trivially easy.
"I know dynamically naming variables is bad practice."
Yes, it sure is! Luckily indexing is trivially easy.

Connectez-vous pour commenter.

Réponses (1)

Bob Thompson
Bob Thompson le 13 Août 2019
I'm going to guess that this line is the incorrect one:
name.(field_names(k,1))(count,1) = data_struct_sorted_unique.(field_names(k,1))(count,1);
If that is not correct, please tell us which line is producing the error.
The problem itself probably comes from the previous line:
name = RouteName_catagories(i,1)
I don't know what RouteName_catagories is, but I'm assuming that it is not a structure class variable, so attempting to call the dot indexing (.fieldname) will not work.
You've probably seen it before, but it is generally considered bad practice to try and dynamically name variables in Matlab, and that it is much simpler, and smoother to use indexing. For your case, instead of trying to name a specific field after each route, just do something like name a field 'name' and then assign it the name value.
Data(i).name = RouteName_catagories(i,1);
Additionally, indexing with structures is most easily accomplished by assigning the index value to the top structure itself, rather than indexing the fields themselves. As a result, getting the information about route 1 is as simple as calling Data(1), which will return a 1x1 structure that contains the name and corresponding data (I'm assuming you attach that).

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by