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

1 回表示 (過去 30 日間)
Dustin Fetterhoff
Dustin Fetterhoff 2023 年 2 月 3 日
編集済み: Matt J 2023 年 2 月 3 日
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

回答 (1 件)

Matt J
Matt J 2023 年 2 月 3 日
編集済み: Matt J 2023 年 2 月 3 日
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))

カテゴリ

Help Center および File ExchangeCell Arrays についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by