Dynamic Assign Field Access in MATLAB Structures

25 ビュー (過去 30 日間)
Teoman
Teoman 2023 年 9 月 28 日
コメント済み: Stephen23 2023 年 10 月 3 日
I have a MATLAB structure called `parameters` containing various fields such as `xl`, `xu`, `error_criterion`, `Cd`, `relative_error`, and `velocity_target`. I'd like to achieve dynamic access to these field values for further processing without manually specifying each field.
Like in my function I want to reasighn the value of Cd = parameters.Cd, relative_error = parameters.relative_error and so on for every single field in the struct in a for loop. How would I be able to go throug with this?
Here's an example of what I've implemented:
function myFunction(parameters)
% Dynamically access structure field values
fieldNames = fieldnames(parameters);
for i = 1:length(fieldNames)
fieldName = fieldNames{i};
fieldValue = parameters.(fieldName);
% Further processing based on 'fieldValue'
end
% Rest of the code
end
Main Script:
function main(parameters)
% Access the variables using the field names in the structure
xl = parameters.xl;
xu = parameters.xu;
error_criterion = parameters.error_criterion;
Cd = parameters.Cd;
relative_error = parameters.relative_error;
velocity_target = parameters.velocity_target;
% Call the function with the structure as an argument
myFunction(parameters);
end
---
  2 件のコメント
Voss
Voss 2023 年 9 月 28 日
"I want to reassign the value of Cd = parameters.Cd, relative_error = parameters.relative_error and so on"
Why do you want to do that? Just leave them in the structure and use them from there.
Stephen23
Stephen23 2023 年 9 月 28 日

https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval

Exactly as Voss wrote, the best approach is to simply access the data from the structure.

サインインしてコメントする。

回答 (1 件)

Bruno Luong
Bruno Luong 2023 年 9 月 28 日
編集済み: Bruno Luong 2023 年 9 月 28 日
clear
s=struct('a',1,'b',2,'c',3)
s = struct with fields:
a: 1 b: 2 c: 3
% this is a dirty command that pops the field values to the workspace
cellfun(@(f) assignin('caller',f,s.(f)), fieldnames(s));
whos
Name Size Bytes Class Attributes a 1x1 8 double b 1x1 8 double c 1x1 8 double s 1x1 528 struct
a
a = 1
b
b = 2
c
c = 3
  7 件のコメント
Bruno Luong
Bruno Luong 2023 年 9 月 29 日
"Your refusal to time the (most likely) most efficient approach is curious, given that the bulk of your comments are about timing."
Again my code is perfectly fair as I explained.
If you want to show some aspect of whatever you defense, please make a code and post it. It is not my job sorry.
I have reached a (partial) conclusion with my test code results, actually it surprises me because in the pass I remember (wrongly) that puffed varables cannot be subjected of JIT accelerator, and obviously in my test shows that it can and very well (not sure I want to go back and check with older version of MATLAB whereas this behavior has changed.)
With that in mind I might reconsider that I would use the puffed recipe assignin in the future as OP request here.
"The original advice is perfectly sound,"
IMO no, if it depends the context then one should ask the context before giving out this general advise. The fault is shared, and mostly yours. OP cannot know the appropriate advise is context dependent, you know it, and clearly did not tell it. I read it wrong because it is wrong.
Stephen23
Stephen23 2023 年 10 月 3 日
s=struct('a',1,'b',2,'c',3);
timeit(@()field_access(s),1)
ans = 5.4982e-04
timeit(@()pop_varaccess(s),1)
ans = 6.1182e-04
function x = field_access(s)
a = s.a;
b = s.b;
c = s.c;
for k = 1:1e6
x = a + b + c;
end
end
function x = pop_varaccess(s)
cellfun(@(f) assignin('caller',f,s.(f)), fieldnames(s));
for k = 1:1e6
x = a + b + c;
end
end

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDesktop についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by