How can I efficiently use an array of patterns to create figure titles?

1 vue (au cours des 30 derniers jours)
Dustin Fetterhoff
Dustin Fetterhoff le 3 Fév 2023
Modifié(e) : Matt J le 3 Fév 2023
I loop through many neurons that come with different structures. The neuron name contains a string pattern that indicates what structure it is and I want to use that to put the name in a figure title. I tried using a dictionary (2022b only), using strfind() and using contains() but couldn't get them to work. I ended up implementing it as a big if-elseif construct but I think there has to be a nicer solution.
regNames = ["Anterior HPC" "Posterior HPC" "Amygdala" "Enthorinal Cortex" "Pararhinal cortex"];
patahp = "u" + ("HR"|"HL"|"AHR"|"AHL");
patphp = "u" + ("PHR"|"PHL");
patamg = "u" + ("AR"|"AL");
patec = "u" + ("ECR"|"ECL");
patph = "uPCL";
patNames = [patahp,patphp,patamg,patec,patph]; % Keys from pattern names
d1 = dictionary(patNames,regNames);
d1("uPCL"); % regular string key works
contains("uHR",patahp); % pattern detection works
d1("uHR"); % Why doesn't pattern detection work in dict keys?
strfind("uHR",patNames) % How could I use this solution?
figure()
cell_name = 'uAHL1_pos_1'; % names to test: 'uAL1_neg_1' 'uHL4_pos_4' 'uECL8_pos_3'
if contains(cell_name,patahp)
title(regNames(1))
elseif contains(cell_name,patphp)
title(regNames(2))
elseif contains(cell_name,patamg)
title(regNames(3))
elseif contains(cell_name,patec)
title(regNames(4))
elseif contains(cell_name,patph)
title(regNames(5))
end

Réponses (1)

Matt J
Matt J le 3 Fév 2023
Modifié(e) : Matt J le 3 Fév 2023
One possibility:
regNames = ["Anterior HPC" "Posterior HPC" "Amygdala" "Enthorinal Cortex" "Pararhinal cortex"];
pats = "u" + ["HR","HL","AHR","AHL","PHR","PHL", "AR","AL", "ECR","ECL","uPCL"];
regnames=repelem(regNames,[4,2,2,2,1]);
cell_name = "uAHL1_pos_1"; % names to test: 'uAL1_neg_1' 'uHL4_pos_4' 'uECL8_pos_3'
tf=arrayfun(@(p)startsWith(cell_name,p),pats)
tf = 1×11 logical array
0 0 0 1 0 0 0 0 0 0 0
assert(nnz(tf)>0,'Pattern not found')
assert(nnz(tf)<2,'Pattern ambiguous')
figure(); title(regnames(tf))

Catégories

En savoir plus sur Cell Arrays dans Help Center et File Exchange

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by