How to Store Variable Names for Struct Titles

8 次查看(过去 30 天)
Hello Community,
I have a script to run various times that when the functions are used outputs a table and other data. Each time I run this, I would like to record the table as a new line in a struct, so I end up with several struct arrays with different data. I have this part covered - but what I am struggling with is getting the struct array to be named as I want ie, I want the struct to be named after the variables that are read in to a particular function - so perhaps this needs to be written as an extra line in the specific function?
The function is called: PODF The struct is called: ORMRes
For example, I call the function as:
[ORM, pStats, T] = PODF(CA, FCON, DF)
Where T is the table. I then write to the struct as follows:
ORMRes.(????)=T(:,:)
Which with a proper name in for the ????'s writes the table in to the struct, but, I want to be able to name the table recorded in the struct, the names of the variables that were used in the PODF function, in this example 'CA', 'FCON', 'DF'
Could anyone advise on the step that I am missing please? I have tried to use fullfile - but no luck. Or, if you think I am just going the wrong way about storing this data, then please just let me know your suggestions for improvement.
Thanks for your help.
Regards,
10B.
  2 个评论
Geoff Hayes
Geoff Hayes 2016-4-10
编辑:Geoff Hayes 2016-4-10
10B - to be clear, you want to dynamically assign the name of the struct member based on the inputs used? So if I were to access this struct as
ORMRes.CA_FCON_DF
then I would get the T that is returned from PODF?
10B
10B 2016-4-11
Hello Geoff,
Thanks for responding. Yes, I believe your description is correct. The bigger picture of what I am doing is creating data output from the PODF function, which will be run several times and with defferent combinations of input variables.
I need to store the various output tables 'T' in a struct so I can access them later to build a contingency table with the results.
I thought that to keep track of the input variables used to create each table, it would be best to assign them as the name for the struct member.
I'm quite inexperienced with writing code, and couldn't think of a better way to handle this - short of writing it all down with pen and paper!
If you have any insight, I would be very grateful for your views.
Kind regards,
10B.

请先登录,再进行评论。

采纳的回答

sam0037
sam0037 2016-4-13
Hi,
To dynamically name the fields of struct depending on the name of the inputs to the function, you can use the ' inputname ' function to get the names of the input. To illustrate this I have attached a script and a function file named 'PODF_Script.m' and 'PODF.m' respectively. The PODF function returns the name of the field to be used in the struct depending on the input names. Finally generate the field names from variables using this link .
  1 个评论
10B
10B 2016-4-13
Thank you for your generous comments and code Abdus, this works perfectly.
I really appreciate it.
Kind regards,
10B.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by