Access elements/fields from a struct

I am generating a struct variable, returned from a SOAP API query, and am struggling to access the elements or fields contained within the struct.
When ever use: I 'disp(structVariable)'
It returns the dimensions of the struct.
I do not know the fieldnames contained in the struct either.
Is there an easy way to access a struct without knowing the fieldnames, then maybe send each element to a text file?

7 件のコメント

Philip  Spratt
Philip Spratt 2013 年 6 月 26 日
Thanks for everyone's input, much appreciated as always.
Using Tom's method, I get the fieldnames returned. However, when it comes to writing the field to a file (fprintf), Matlab throws an error saying the fieldname does not exist, even though it just confirmed all the fieldnames??
Any ideas why this might be?
See the error below:
Sending eFetch query to PubMed. Awaiting response... Field names: MedlineCitation: [1x1 struct] PubmedData: [1x1 struct]
??? Reference to non-existent field 'PubmedData'.
Error in ==> databaseresultsgui_v2ps>btnLitSearch_Callback at 182 fprintf(fileID,abstr.PubmedData)
Error in ==> gui_mainfcn at 96 feval(varargin{:});
Error in ==> databaseresultsgui_v2ps at 44 gui_mainfcn(gui_State, varargin{:});
Error in ==> guidemfile>@(hObject,eventdata)databaseresultsgui_v2ps('btnLitSearch_Callback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback
Tom
Tom 2013 年 6 月 26 日
what is displayed if you enter
abstr
in the command line?
Philip  Spratt
Philip Spratt 2013 年 6 月 26 日
abstr =
PubmedArticle: [1x1 struct]
Tom
Tom 2013 年 6 月 26 日
So PubmedData doesn't exist in the abstr variable ; where does this come from?:
Field names: MedlineCitation: [1x1 struct] PubmedData: [1x1 struct]
Philip  Spratt
Philip Spratt 2013 年 6 月 26 日
When I use the method you described originally, with the variable abstr, the fieldnames are: MedlineCitation: [1x1 struct] PubmedData: [1x1 struct]
When I just enter abstr, PubmedArticle: [1x1 struct], is returned??
There are even more struct fieldnames as you go through PubMedArticle.
Tom
Tom 2013 年 6 月 26 日
I think you will need to post a larger code snippet, maybe as a new question.
Philip  Spratt
Philip Spratt 2013 年 6 月 26 日
Realised I needed to use the below. There are multiple struct variables at each level with the different information (nested struct). I suppose the data is there, just thought it may have been easier to output as a simple text file without having to go through each struct (array).
abstr.PubmedArticle(1).MedlineCitation abstr.PubmedArticle(1).PubmedData

サインインしてコメントする。

 採用された回答

Iain
Iain 2013 年 6 月 25 日

4 投票

Sounds like you have something like:
b = dir('C:\Windows\');
b(1)
names = fieldnames(b(1));
for i = 1:numel(names)
value{i} = getfield(b(1),names{i})
end

1 件のコメント

Jan
Jan 2018 年 2 月 8 日
Or easier:
value = struct2cell(b(1));

サインインしてコメントする。

その他の回答 (3 件)

Tom
Tom 2013 年 6 月 25 日
編集済み: Jan 2018 年 2 月 8 日

5 投票

Structures can be referenced dynamically, i.e. using variable names not strings the user has written. In this case you have to put brackets around the dynamic reference.
So
s.a
is the same as
s.('a')
So then,
n = 'a';
s.(n)
can be used instead. the FIELDNAMES function can be used to get the field names of a structure.
S.a = 'test';
S.b = 1;
S.c = rand(2);
fNames = fieldnames(S);
for n = 1:length(fNames)
disp(S.(fNames{n}))
end
You can then use FPRINTF or something similar to write to a text file.

1 件のコメント

mohammad alhayani
mohammad alhayani 2018 年 2 月 8 日
thanks alot... ^_^ you helped me with my project.... so thanks..

サインインしてコメントする。

Muthu Annamalai
Muthu Annamalai 2013 年 6 月 25 日

0 投票

In addition to the excellent answers posted by @Iain, and @Tom, you may also want to look at MATLAB support for JSON from the FileExchange http://www.mathworks.com/matlabcentral/fileexchange/33381-jsonlab-a-toolbox-to-encodedecode-json-files-in-matlaboctave
Goodluck!
Image Analyst
Image Analyst 2013 年 6 月 25 日

0 投票

You can just type the variable name on the command line, or as just a single line in a script, to learn what the field names are. No semicolon or anything. Just the variable name itself:
structVariable
It will show you what the field names are, and their values. Then, once you know that, you can write code to write them out to a text file using fprintf(). If you don't know the fields and the fields change all the time, then you can use code the others gave you. But I don't think this is your case - in your case the variable returned from your SOAP API query will have the same fields every time, right? What does it show when you put "structVariable" on its own line?

カテゴリ

ヘルプ センター および File ExchangeStructures についてさらに検索

質問済み:

2013 年 6 月 25 日

コメント済み:

Jan
2018 年 2 月 8 日

Community Treasure Hunt

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

Start Hunting!

Translated by