How can I access a structure by using purely strings?

My current code requests a structure from the user as an input in the form of its name 'file'. I am trying to access this structure in my function with the string 'file'. For example
structure's name is file
i can access the fields in it by writing file.field. I can also do n = 'field'; and access the same thing doing file.(n). I cannot however do y = 'file' and access the same field doing (y).(n). Is there a way around this?
Thanks

3 Kommentare

If you have many named structures you're trying to iterate over, like 'file1', 'file2', etc, why don't you put them all into subfields of a root struct?
root.file1.field could be accessed as root.(y).(n) as you pointed out.
Xavier
Xavier am 16 Jun. 2016
Yes that would be nice, but unfortunately that is not the case. I only have 1 structure, and I cannot guaranteed what the "root" structure name is.
Stephen23
Stephen23 am 16 Jun. 2016
Bearbeitet: Stephen23 am 19 Jun. 2019
@Xavier: there are ways, but MATLAB has a whole page recommending against this:
Read this to know some reasons for avoiding doing this:
If you actually describe the whole situation a bit more, then we can advise you better ways to achieves what you want to do. Are you using load ?

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Gefragt:

am 16 Jun. 2016

Bearbeitet:

am 19 Jun. 2019

Community Treasure Hunt

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

Start Hunting!

Translated by