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

조회 수: 2 (최근 30일)
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
Stephen23
Stephen23 2022년 7월 16일
편집: Stephen23 2022년 7월 16일
"%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')
Odysseas Makariou
Odysseas Makariou 2022년 7월 16일
okay I will try thank you

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2022년 7월 16일
편집: Stephen23 2022년 7월 16일
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by