how to compare string array with the names of structure's fields. (2024)

26 views (last 30 days)

Show older comments

Sara on 2 Jul 2018

  • Link

    Direct link to this question

    https://www.mathworks.info/matlabcentral/answers/408436-how-to-compare-string-array-with-the-names-of-structure-s-fields

  • Link

    Direct link to this question

    https://www.mathworks.info/matlabcentral/answers/408436-how-to-compare-string-array-with-the-names-of-structure-s-fields

Commented: Sara on 3 Jul 2018

Accepted Answer: Paolo

I have a structure with 12 fields and a string array named variables containing 4 values. I want to compare all values in my string arrays with the names of my structure fields, and if the fields' name and the string value were the same, return values of that field as an output. I try strcmp command to compare my string array with the fields' name in a for loop.(for i = 1:numel(variables)) then I use switch and case but I have no idea how to return the field values as an output when the statement is true. (I think I should use getfield command but I don't know how). could you please help me in this regard. thank you.

1 Comment

Show -1 older commentsHide -1 older comments

Adam on 2 Jul 2018

Direct link to this comment

https://www.mathworks.info/matlabcentral/answers/408436-how-to-compare-string-array-with-the-names-of-structure-s-fields#comment_584909

  • Link

    Direct link to this comment

    https://www.mathworks.info/matlabcentral/answers/408436-how-to-compare-string-array-with-the-names-of-structure-s-fields#comment_584909

You can use dynamic strings to get data out of a struct field e.g.

str = 'SomeStructFieldName';

value = myStruct.( str );

Sign in to comment.

Sign in to answer this question.

Accepted Answer

Paolo on 2 Jul 2018

  • Link

    Direct link to this answer

    https://www.mathworks.info/matlabcentral/answers/408436-how-to-compare-string-array-with-the-names-of-structure-s-fields#answer_327172

S.a = {'First field'};

S.b = {'Second field, entry 1','Second field,entry 2'};

S.c = {1,2,3};

mystring = {'a','game','matlab','c'};

[~,indx] = ismember(fieldnames(S),mystring);

all = arrayfun(@(x) getfield(S,mystring{x}),indx(indx>0),'un',0);

all{:} =

{'First field'}

{[1]} {[2]} {[3]}

3 Comments

Show 1 older commentHide 1 older comment

Sara on 3 Jul 2018

Direct link to this comment

https://www.mathworks.info/matlabcentral/answers/408436-how-to-compare-string-array-with-the-names-of-structure-s-fields#comment_585186

  • Link

    Direct link to this comment

    https://www.mathworks.info/matlabcentral/answers/408436-how-to-compare-string-array-with-the-names-of-structure-s-fields#comment_585186

Thanks for our answer it is totally true. However I have two questions: Firstly I don't understand the last line very well (I am not expert about matlab); what does the function of @(x) ?

secondly what the code should be written if I want to save the fields' values as well as the fields names in the form of a table (now 'all' just contains the values of the fields not their names). I mean considering the field names as a header of a table.

I appreciate your help.

Stephen23 on 3 Jul 2018

Direct link to this comment

https://www.mathworks.info/matlabcentral/answers/408436-how-to-compare-string-array-with-the-names-of-structure-s-fields#comment_585189

  • Link

    Direct link to this comment

    https://www.mathworks.info/matlabcentral/answers/408436-how-to-compare-string-array-with-the-names-of-structure-s-fields#comment_585189

Edited: Stephen23 on 3 Jul 2018

@Sahar: the @(x) defines an anonymous function in x:

Paolo on 3 Jul 2018

Direct link to this comment

https://www.mathworks.info/matlabcentral/answers/408436-how-to-compare-string-array-with-the-names-of-structure-s-fields#comment_585194

T = table(all{:});

T.Properties.VariableNames = mystring(indx(indx>0));

T =

1×2 table

a c

_____________ _________________

'First field' [1] [2] [3]

Sign in to comment.

More Answers (1)

Stephen23 on 3 Jul 2018

  • Link

    Direct link to this answer

    https://www.mathworks.info/matlabcentral/answers/408436-how-to-compare-string-array-with-the-names-of-structure-s-fields#answer_327268

  • Link

    Direct link to this answer

    https://www.mathworks.info/matlabcentral/answers/408436-how-to-compare-string-array-with-the-names-of-structure-s-fields#answer_327268

Edited: Stephen23 on 3 Jul 2018

I would use struct2cell to get a cell array, then used basic MATLAB indexing to obtain the "fields" (rows of the cell array) that you want. Using a cell array makes it easy to use cell2table to generate a table from the cell array and the fieldnames:

>> S.a = '1st';

>> S.b = {'2ndOne','2ndTwo'};

>> S.c = 3;

>> F = fieldnames(S);

>> C = struct2cell(S);

>> lookfor = {'a','hello','world','c'};

>> [idx,idy] = ismember(lookfor,F);

>> idz = idy(idx)

>> T = cell2table(C(idz,:),'VariableNames',F(idz))

3 Comments

Show 1 older commentHide 1 older comment

Sara on 3 Jul 2018

Direct link to this comment

https://www.mathworks.info/matlabcentral/answers/408436-how-to-compare-string-array-with-the-names-of-structure-s-fields#comment_585225

  • Link

    Direct link to this comment

    https://www.mathworks.info/matlabcentral/answers/408436-how-to-compare-string-array-with-the-names-of-structure-s-fields#comment_585225

I write my code completely according yours; but I got this error

Error using cell2table (line 57) The VariableNames property must contain one name for each variable in the table.

my code is something like this:

battery.Cycle =[1,2];

battery.Voltage = [3,4];

battery.Current = [4,5];

battery.DischargeCapacity = [5,6];

variables = {'Cycle','Voltage','Current','DischargeCapacity'};

Fname = fieldnames(battery );

Cell = struct2cell(battery );

[indx,indy] = ismember(variables,Fname );

indz = indy(indx )

Table = cell2table(Cell(indz,:),'VariableNames',Fname(indz));

Stephen23 on 3 Jul 2018

Direct link to this comment

https://www.mathworks.info/matlabcentral/answers/408436-how-to-compare-string-array-with-the-names-of-structure-s-fields#comment_585226

  • Link

    Direct link to this comment

    https://www.mathworks.info/matlabcentral/answers/408436-how-to-compare-string-array-with-the-names-of-structure-s-fields#comment_585226

Edited: Stephen23 on 3 Jul 2018

@Sahar: you are right, I think the first input needs to be transposed:

Table = cell2table(Cell(indz,:).','VariableNames',Fname(indz));

According to the cell2table help "Each column of C provides data for a table variable", whereas struct2cell returns one row for each field, so a transpose is required.

Sara on 3 Jul 2018

Direct link to this comment

https://www.mathworks.info/matlabcentral/answers/408436-how-to-compare-string-array-with-the-names-of-structure-s-fields#comment_585242

  • Link

    Direct link to this comment

    https://www.mathworks.info/matlabcentral/answers/408436-how-to-compare-string-array-with-the-names-of-structure-s-fields#comment_585242

Many Thanks for your help.

Sign in to comment.

Sign in to answer this question.

See Also

Categories

MATLABLanguage FundamentalsData TypesData Type Conversion

Find more on Data Type Conversion in Help Center and File Exchange

Tags

  • string
  • structures
  • getfield

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


how to compare string array with the names of structure's fields. (11)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

Europe

Asia Pacific

Contact your local office

how to compare string array with the names of  structure's  fields. (2024)
Top Articles
Latest Posts
Article information

Author: Ouida Strosin DO

Last Updated:

Views: 6298

Rating: 4.6 / 5 (76 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Ouida Strosin DO

Birthday: 1995-04-27

Address: Suite 927 930 Kilback Radial, Candidaville, TN 87795

Phone: +8561498978366

Job: Legacy Manufacturing Specialist

Hobby: Singing, Mountain biking, Water sports, Water sports, Taxidermy, Polo, Pet

Introduction: My name is Ouida Strosin DO, I am a precious, combative, spotless, modern, spotless, beautiful, precious person who loves writing and wants to share my knowledge and understanding with you.