How to merge structures with the same field names?
9 views (last 30 days)
Show older comments
Hello,
Could I get some help in determining the root cause of an error I keep getting? The error is: Argument to dynamic structure reference must evaluate to a valid field name. I am trying to follow the second solution in this previous post https://www.mathworks.com/matlabcentral/answers/365191-is-it-possible-to-concatenate-structures-with-the-same-fields-in-to-one-super-structure but the chosen solution doesn't quite work out for my application. Here is the code I am trying to use and modify to suit my purpose:
function S = CatStructFields(S, T, dim)
fields = fieldnames(S);
for k = 1:numel(fields)
aField = fields(k);
S.(aField) = cat(dim, S.(aField), T.(aField));
end
full_data = CatStructFields(c, c1, 1)
I am trying to load saved historical workspace data from several .mat files and merge the variables together to create arrays containing the information stored previously. The information is stored in an array of structures where each structure has the same field names and types. I am then trying to merge them all together and pull the field names and values out of the structure and have them exist as workspace variables exactly the way they were when they were initially saved. I do this because I then run a plotting script, that I would rather not modify, that uses these variable names to produce the graphical information. I use MATLAB to gather raw data from an RF lab station that tests wireless equipment. The main MATLAB script is able to gather all of this information for either a single frequency value or an array of frequency values. The plotting script is then run and is able to plot graphs for all the included frequency points specified in the main script. The script I am trying to create is intended to take the workspace results of values obtained running the main script for single frequency points and merge them to appear as if they were obtained from running the main script with multiple frequency values specified. I am trying to get the resulting workspace to always be in the same configuration that the plotting script accepts. My script code is written below and I will also attach some graphics that show the variable types and sizes to help with the troubleshooting efforts. Take a look at the FreqRange variable to see if the image corresponds to a single or multiple frequency points.
[file, path, index] = uigetfile('C:\initial_pathname','Select Workspace Files for Individual Frequencies','MultiSelect','on');
if (iscell(file) || path ~= 0 || index ~= 0)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/170430/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/170432/image.png)
for k=1:length(file)
single_freq(k) = load(char(strcat(path,'\',file(1,k))));
end
end
field_name = fieldnames(single_freq);
for k=1:numel(field_name)
temp_field = field_name(k);
test.(temp_field) = cat(2,single_freq(1),single_freq(2));
end
1 Comment
per isakson
on 15 Feb 2018
TL;NR, but I guess that "The error is: Argument to dynamic structure reference must evaluate to a valid field name" is because the value is a scalar cell array containing the name rather than the name itselft.
Accepted Answer
More Answers (0)
See Also
Categories
Find more on Structures in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!