필터 지우기
필터 지우기

Dynamic Assign Field Access in MATLAB Structures

조회 수: 16 (최근 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 CenterFile Exchange에서 Debugging and Analysis에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by