look for partial variable name match

조회 수: 11 (최근 30일)
Amir
Amir 2012년 6월 27일
Hi,
Anyone can help me with partial variable name match? Here is the problem. I have a .mat file with multiple cases in a structure format: load file.mat variables: C1.variables C2.variables etc the name of variables have some prefixes: LCM_variable1 so it becomes: C1.LCM_variable1 I need to find variable1 from C1.LCM_variable1 in the file while the name of variables of interest are defined in a string list as inputs={'variabl1', 'variable2'} but the prefixes are not known.
Thanks

답변 (2개)

Image Analyst
Image Analyst 2012년 6월 27일
Use fieldnames() to list the fields (members) of a structure. Then you can examine them for certain names. You might use strfind() or ismember(). If you can't figure it out, even after using the help, then write back to us.
  댓글 수: 2
Amir
Amir 2012년 6월 27일
Thanks.
What function could I use to find a name match for a variable with pre-fix or suffix? I have a string arrays of names and need to search for a variable say variable1 that is embedded in something like FLC_C_variable1 ?
Image Analyst
Image Analyst 2012년 6월 28일
Try strfind() or strcmpi().

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


Kye Taylor
Kye Taylor 2012년 6월 28일
편집: Kye Taylor 2012년 6월 28일
I assume that by "a string arrays of names" you mean a cell, call it C, whose contents are strings. For example, something like
C = {'a string', 'another string', 'FLC_C_variable1','FLS_C_variable2_plusMore', 'FLC_C_variable3Three'};
To find strings containing the substring 'variable1', another approach in addition to those given above would be to use the command
outCell = cellfun(@(s)regexp(s,'variable1'),C,'uniform',0)
which will return a cell, outCell, that is the same size as the original cell array C. Non-empty cells in outCell contain the index of the substring 'variable1' that was found in the corresponding cell in C. (If that is confusing, run the two commands above, then let me know if I can clarify.)
You can create an array like C from a structure's fieldnames using
C = fieldnames(someStructure);

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by