1

I have this from the matlab reference manual

value = getfield(struct, 'field')

where struct is a 1-by-1 structure, returns the contents of the specified field, equivalent to

value = struct.field

how can I do the opposite

getStringName(struct.field) which return 'field'

also if it is possible to point at the field in numerical way similar to an array

like struct{1} for field 1 field

edit

if I do the follow

structName(1) I get a list of field names, and dimentions

 Speed: [2244x1 double] Time: [2244x1 double] ... and so on 

I want to grab the title speed as string, and if possible

structName(1).filed(1) for Speed without doing structName(1).Speed

I want to print each field into a file with the field name!

so if I do

for i=1:sizeOfStruct printToFile(structName(i)); %<=== accessing field by index, problem 2 end function printToFile(structField) structFieldStr = getStrFiledName(structField); %<=== obtain field string, main problem filename = strcat(fileLoc, '/profile/', structFieldStr, '.dat'); %... then open file and dump everything end 
3
  • Unless it appears left of an assignment, struct.field is just a value, for example 2.5, and no longer has any ties to the struct object. Commented Sep 24, 2013 at 19:09
  • Does the fieldnames function give what you want, or are you trying to find the field which contains a certain value? You can also try inputname, not sure if that works with structure members Commented Sep 24, 2013 at 19:26
  • 1
    fieldnames gave me one part, whats left is grabbing that field by its location, or field name Commented Sep 24, 2013 at 19:34

1 Answer 1

1

Not the complete answer to your question, but this should get you started:

s.a = 11; s.b = 22; s.c = 33; names = fieldnames(s); for i = 1:length(names) fprintf('Field %s = %g\n', names{i}, s.(names{i})) end 

Result:

Field a = 11 Field b = 22 Field c = 33 

Note the syntax to programmatically access a field name: s.(name), where s is a struct and name is a string.

Sign up to request clarification or add additional context in comments.

2 Comments

Reference to non-existent field 'fieldnames'. is the error I get if I tried s.fieldnames, however I used fieldnames(s), however s.(name) still not working 'Undefined function or variable 'a'.'
Sorry, I was still editing out some bugs, should work now. That is what you get when posting untested code ...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.