how to find the filed in structure in which a word belongs to?

3 views (last 30 days)
Hello,
I have a structure that looks like this:
'4412p100.txt' 101x2 table
'FX74-Cl5-140-MOD.dat' 79x2 table
'FX74_CL5_140.dat' 87x2 table
'MultiElementFrontWing.txt' 149x2 table
'NACA4312.dat' 99x2 table
'NACA7412.dat' 100x2 table
'ch10sm.dat' 79x2 table
.
.
.
the user is supposed to select a name from a drop down list, and I want to locate the number of the filed that name belongs to in the structure so that it can be used in further computations
Any ideas how i can achieve that?
I was thinking of representing each name with a number, however the order in the structure will not necessarily be the same as the order in the drop down button.
Also, is there a way to automatically update the drop down button to include anyb new entries saved in the structure?
Thanks!
  5 Comments
Stephen23
Stephen23 on 16 Jul 2022
Edited: Stephen23 on 16 Jul 2022
"%the first two elements are '.' and '..'"
No, in general they are not. They might be sometimes (depending on the filesystem and filenames and the DIR match string), but there is absolutely no guarantee of this. For robust code use ISMEMBER or SETDIFF.
Your code would be much more robust if you included a match string (with wildcard) to match the filenames. Then you can avoid fiddling around with the dot-directory names.
Avoid CD in code. It is slow and makes debugging harder.
Why create a new structure when you already have the one returned by DIR?
I recommend using function syntax rather than command syntax:
P = '.'; % absolute or relative path
D ='aerofoil_research';
S = dir(fullfile(P,D,'*.*'));
for k = 1:numel(S)
F = fullfile(S(k).folder,S(k).name);
S(k).data = readtable(F);
end
save('aerofoil_database','S')

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 16 Jul 2022
Edited: Stephen23 on 16 Jul 2022
You can get the index of a specific name using a comma-separated list and STRCMPI (or MATCHES, etc):
For example, where S is your structure:
want = '4412p100.txt'; % the name you want find
idx = strcmpi(want,{S.name});
assert(nnz(idx)==1,'exactly one name must match')
S(idx).data

More Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!