MATLAB: Structure Indexing to Find Words

structures

clear
load('dict_perms.mat')
dict=6;
dict_perms=dict_perms{6};
dict_char=char(dict_perms+64);
dict_size=length(dict_perms(:,1));
for i=1:dict_size
dict_lex.(dict_char(i,1)).(dict_char(i,2)).(dict_char(i,3)).(dict_char(i,4)).(dict_char(i,5)).(dict_char(i,6))=uint16(i);
end
%the next letters possible from a series of letters can be found with
letters=fieldnames(dict_lex.('A').('B').('H'));
%I want words from possible letters
%like dict_lex.(['A','B']).(['D','B']).(['F','H']).(['O','F']).(['R','M']).(['S','K'])
%could be found with a for loop but it is too time consuming
fieldnames(dict_lex.('A'));
fieldnames(dict_lex.('B'));
I need to find all possible words that have letters in certain parts of the word. This could be done with a for loop but that is too time consuming. Can anyone point me in the right direction?

Best Answer

Dynamically accessing the fieldnames of nested structures is likely to be a very inefficient solution, and is far too complex for the task. You would be much better off simply accessing the data directly from the char array using logical indexing, or converting to a cell array of char vectors and then using standard, efficient built-in tools such as regular expressions, or even strncmp. For example:
>> DC = char(dict_perms{6}+64);
>> idx = DC(:,1)=='A' & DC(:,2)=='B' & DC(:,3)=='H';
>> DC(idx,:)
ans = ABHORS
To match multiple character you could add more logical conditions to the index:
>> idx = ...
(DC(:,1)=='A' | DC(:,1)=='B') & ...
(DC(:,2)=='B' | DC(:,2)=='D') & ...
(DC(:,3)=='H' | DC(:,3)=='F') & ...
(DC(:,4)=='O' | DC(:,4)=='F') & ...
(DC(:,5)=='R' | DC(:,5)=='M') & ...
(DC(:,6)=='S' | DC(:,6)=='K');
>> DC(idx,:)
ans = ABHORS
But actually it would be much easier and more versatile to use regular expressions:
>> DS = cellstr(DC);
>> idx = ~cellfun('isempty',regexp(DS,'^[AB][DB][FH][OF][RM][SK]','once'));
>> DC(idx,:)
ans = ABHORS
It is easy with regular expressions to return the complete matching words, the matching letters, their indices, or the trailing letters... it all depends on what you want. If you give a more accurate description of the data that you need then I can help you with the regular expression.