Input char to fieldnames as argument
Show older comments
Hi, I am trying find out all names in field in structure. I have name of structure in class char and fieldnames need as argument structure. Basicaly my char without quotes. If I put as argument to fieldnames char I get error: Undefined function 'fieldnames' for input arguments of type 'char'.
Example:
Name of structure is: F05
Char which I have is: 'F05'
fieldnames('F05') % Undefined function 'fieldnames' for input arguments of type 'char'.
fieldnames(F05) % Names in field (That what I want)
So I need convert all chars automatically to names of structure which i will use in fieldnames.
Thank you for your help
5 Comments
"So I need convert all chars automatically to names of structure which i will use in fieldnames."
Ugh, no, that is the last thing that you need to do. Import your data properly and avoid this situation entirely is what you need to do. Dynamically accessing variables names is how beginners force themselves into writing slow, complex, buggy code. Read this to know why:
It is much more efficient to use indexing, fieldnames, or the columns/rows of a table.
So you want to convert the name of a variable held into a char array into the variable itself.
This is universally a bad idea and an indication that your program structure has gone wrong somewhere. Can you explain a bit how and why you got there so we can correct that rather than putting a band-aid on a bad design?
I presume that if you have F05, you probably also have F01, F02, etc. Why aren't all these elements of the array F?
Eduard Sláma
on 19 Jun 2018
Guillaume
on 19 Jun 2018
"this solution is use function evalin"
This is what I called putting a band-aid on a bad design. It would have been a lot more beneficial to you if you had tried to solve the initial bad design. Having gone with the band-aid solution, you'll end up using more and more band-aids until your program is just a badly held together collection of band-aids. One of them will fall off and hell will break loose!
Answers (0)
Categories
Find more on Structures in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!