Create a new structure using a function in a for loop.

I have a structure, and I want to use the values of one of the fields as the names of new structures. So, for instance, I have structure "struct" with the field "field" that has the following values that are strings:
'value1'
'value2'
'value3'
I have a function whose output is a new structure that I want to be the name of the field values in my first structure (so, value1, value2, and value3) and whose input I also want to be the name of the field values in my first structure. I'm trying to create the new structures (named value1, value2, and value 3 using a for loop like this
for j = 1 length(struct.field)
[struct.field{j}] = function(struc.field{j})
end
The error I'm getting is
Dot indexing is not supported for variables of this type.
Other than the for loop portion, my function works as long as I manually specify the new structure name. I'm trying to avoid manually specifying the new structure name if possible.
Can I create new structures using a function in a for loop like what I've been trying? If so, what do I need to do to fix my method?
Thanks!

5 Comments

Show us the full code.
Here is my full code for this function:
function [input_general,...
velocity_raw,...
velocity_raw_outlet_landside,...
velocity_raw_outlet_riverside,...
velocity_raw_chamber_riverculvert_riverport,...
velocity_raw_chamber_riverculvert_landport,...
velocity_raw_chamber_landculvert_riverport,...
velocity_raw_chamber_landculvert_landport] = ...
read_adv_file6(input_general)
if strcmp(input_general{1}.velocity_test,'yes') == 1
disp('Velocity files found. Reading velocity files...')
velocity_raw{1}.velocity_files = dir(strcat(pwd,'\',input_general{1}.raw_data_daily_directory,'\',input_general{1}.raw_data_test_directory,'\','*.vna'));
count = 1;
for j = 1:length(velocity_raw{1}.velocity_files)
velocity_raw{1}.file_name{j} = velocity_raw{1}.velocity_files(j).name;
if contains(velocity_raw{1}.file_name{j},'LS') == 1
velocity_raw_outlet_landside{1}.velocity_exist = 'yes';
velocity_raw_outlet_landside{1}.file = velocity_raw{1}.file_name{j};
disp(['Landside Culvert Outlet Velocity File: ',velocity_raw{1}.file_name{j}])
velocity_location{1}.velocity_location{count} = 'velocity_raw_outlet_landside';
count = count + 1;
elseif contains(velocity_raw{1}.file_name{j},'RS') == 1
velocity_raw_outlet_riverside{1}.velocity_exist = 'yes';
velocity_raw_outlet_riverside{1}.file = velocity_raw{1}.file_name{j};
disp(['Riverside Culvert Outlet Velocity File: ',velocity_raw{1}.file_name{j}])
velocity_location{1}.velocity_location{count} = 'velocity_raw_outlet_riverside';
count = count + 1;
elseif contains(velocity_raw{1}.file_name{j},'RR') == 1
velocity_raw_chamber_riverculvert_riverport{1}.velocity_exist = 'yes';
velocity_raw_chamber_riverculvert_riverport{1}.file = velocity_raw{1}.file_name{j};
disp(['Riverside Culvert, Riverside Port Velocity File: ',velocity_raw{1}.file_name{j}])
velocity_location{1}.velocity_location{count} = 'velocity_raw_chamber_riverculvert_riverport';
count = count + 1;
elseif contains(velocity_raw{1}.file_name{j},'RL') == 1
velocity_raw_chamber_riverculvert_landport{1}.velocity_exist = 'yes';
velocity_raw_chamber_riverculvert_landport{1}.file = velocity_raw{1}.file_name{j};
disp(['Riverside Culvert, Landside Port Velocity File: ',velocity_raw{1}.file_name{j}])
velocity_location{1}.velocity_location{count} = 'velocity_raw_chamber_riverculvert_landport';
count = count + 1;
elseif contains(velocity_raw{1}.file_name{j},'LR') == 1
velocity_raw_chamber_landculvert_riverport{1}.velocity_exist = 'yes';
velocity_raw_chamber_landculvert_riverport{1}.file = velocity_raw{1}.file_name{j};
disp(['Landside Culvert, Riverside Port Velocity File: ',velocity_raw{1}.file_name{j}])
velocity_location{1}.velocity_location{count} = 'velocity_raw_chamber_landculvert_riverport';
count = count + 1;
elseif contains(velocity_raw{1}.file_name{j},'LL') == 1
velocity_raw_chamber_landculvert_landport{1}.velocity_exist = 'yes';
velocity_raw_chamber_landculvert_landport{1}.file = velocity_raw{1}.file_name{j};
disp(['Landside Culvert, Landside Port Velocity File: ',velocity_raw{1}.file_name{j}])
velocity_location{1}.velocity_location{count} = 'velocity_raw_chamber_landculvert_landport';
count = count + 1;
else
disp(['Velocity file ',velocity_raw{1}.file_name_with_extension{j},' does not follow a known naming convention. Skipping...'])
end
end
velocity_location{1}.velocity_location = string(velocity_location{1}.velocity_location);
%%%%%%%%% This is what I'm trying to fix.
for j = 1:length(velocity_location{1}.velocity_location)
[velocity_location{1}.velocity_location(j)] = read_single_adv_file(input_general,velocity_location{1}.velocity_location(j));
end
%%%%%%%%%
%%%%%%%%% This section works, but I'm trying to replace the repetition with
%%%%%%%%% a for loop.
% [velocity_raw_outlet_landside] = read_single_adv_file(input_general,velocity_raw_outlet_landside);
%
% [velocity_raw_outlet_riverside] = read_single_adv_file(input_general,velocity_raw_outlet_riverside);
%
% [velocity_raw_chamber_riverculvert_riverport] = read_single_adv_file(input_general,velocity_raw_chamber_riverculvert_riverport);
%
% [velocity_raw_chamber_riverculvert_landport] = read_single_adv_file(input_general,velocity_raw_chamber_riverculvert_landport);
%
% [velocity_raw_chamber_landculvert_riverport] = read_single_adv_file(input_general,velocity_raw_chamber_landculvert_riverport);
%
% [velocity_raw_chamber_landculvert_landport] = read_single_adv_file(input_general,velocity_raw_chamber_landculvert_landport);
%%%%%%%%%
else
disp('No velocity files found. Skipping velocity processing.')
end
end
I'm hoping showing the entire code for this function will be sufficient to understand what I'm trying to do because I have a significant amount of other code that feeds into this function.
@Stephen23 Thank you for the links!
The first link explains why to not dynamically name variables. I will look into that further.
The second link shows how to generate field names from variables. Is the second link the preferred approach or just about showing how to dynamically name variables, which is bad practice?
Thanks!
"The second link shows how to generate field names from variables."
Correct.
"Is the second link the preferred approach..."
"preferred": maybe. Dynamic fieldnames might be quite a suitable approach... or perhaps not. Or perhaps a table... Or a cell array... Or an ND array. It depends very much on your data (e.g. sizes, types, how they are structured, etc), what processing you will do on them, etc.
In general fieldnames (just like variable names) should not contain meta-data (your 'value1', 'value2', etc. are probably meta-data). Meta-data should be stored in a variable, not in its name. Perhaps a structure array might be a good choice for storing that data:
S(1).data = [..];
S(1).name = 'value1';
S(2).data = [..];
S(2).name = 'value2';
..
"or just about showing how to dynamically name variables, which is bad practice?"
Generating fieldnames from variables has nothing (directly) to do with dynamic variable names. Fields are not variables.

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Release

R2021b

Asked:

on 11 May 2022

Edited:

on 12 May 2022

Community Treasure Hunt

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

Start Hunting!