MATLAB Answers

Searching structure that has all caps field names with list that varies in case.

31 views (last 30 days)
Georgia Kennedy
Georgia Kennedy on 23 Feb 2021
Commented: Georgia Kennedy on 23 Feb 2021
Hello,
I am comparing two separate files of data and have loaded the .mat files into structs. One file has field names that are in all capital letters. Ideally I just want to ignore the case altogether. Hpwever, I am unsure how to do this and so I have tried using:
x = upper(load('filename.mat'))
%and
x= load('filename.mat')
y=upper(x)
To change all the fields to uppercase. Niether of these work. ( I also changed the search variables to match case for this)
My code looks like this:
myVars = {'Abc_B', 'Aaa_A', 'B_Ahh', 'Ba_Bcc', 'Dea_C_CC' };
b_struct = load('filename1.mat'); %all upper case
f_struct = load('filename2.mat'); %variable case
for iVar=1:numel(myVars)
f_sp = f_struct.(myVars{iVar});
b_sp = b_struct.dArray.(myVars{iVar});
...
end
And the issue happens when b_struct tries to read in 'B_Ahh', because b_struct the field is 'B_AHH'
The error that matlab gives is this:
Reference to non-existent field 'B_Ahh'.
Error in final_dataPrep
b_sp = b_struct.dArray.(myVars{iVar});
But it outputs previous fields whose case match my search words.
Ideally, I just want to ignore the case altogether.
I am lost and frustrated about this. Any guidance would be greatly appreciated!

Answers (1)

Fangjun Jiang
Fangjun Jiang on 23 Feb 2021
Edited: Fangjun Jiang on 23 Feb 2021
for iVar=1:numel(myVars)
b_sp = b_struct.dArray.(upper(myVars{iVar}));
end
  3 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!