MATLAB Answers


Name Structure in a for loop

Asked by Ernest van Niekerk on 13 Aug 2019
Latest activity Answered by Bob Nbob
on 13 Aug 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);
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 Comment

"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.

Sign in to comment.

1 Answer

Answer by Bob Nbob
on 13 Aug 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).


Sign in to comment.