Rename a struct with a fieldname of another struct
2 ビュー (過去 30 日間)
表示 古いコメント
Hi, I want to load a struct and the name of the struct is for example 'abc' and after that i want to load some fields of the struct like
data_array(1) = abc.data_1
data_array(2) = abc.data_2
but now my problem is that the struct isn't allways called abc...
so i want to use the name of the struct however it's called to load data_1 and data_2 in this case
回答 (1 件)
Robert U
2022 年 8 月 25 日
Hi Jan,
it is good practice to load external data into a structure array such that the variable name is connected to the actual function/script whereas the top fieldname is the name of the saved struct. Then, you can use fieldnames()-command to retrieve the name of the loaded struct and continue work on that.
inputStruct = load(myFilename) % load file
fieldsInputStruct = fieldnames(inputStruct) % retrieve the name of the saved struct
inputStruct.(fieldsInputStruct{1}) % use dynamic expressions to address the fieldnames
See for more information:
- https://de.mathworks.com/help/matlab/matlab_prog/generate-field-names-from-variables.html
- https://de.mathworks.com/help/matlab/ref/load.html?s_tid=doc_ta#d123e846287
- https://de.mathworks.com/help/matlab/ref/fieldnames.html?s_tid=doc_ta
Kind regards,
Robert
2 件のコメント
参考
カテゴリ
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!