Dynamic Assign Field Access in MATLAB Structures

24 visualizaciones (últimos 30 días)
Teoman
Teoman el 28 de Sept. de 2023
Comentada: Stephen23 el 3 de Oct. de 2023
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 comentarios
Voss
Voss el 28 de Sept. de 2023
"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 el 28 de Sept. de 2023

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.

Iniciar sesión para comentar.

Respuestas (1)

Bruno Luong
Bruno Luong el 28 de Sept. de 2023
Editada: Bruno Luong el 28 de Sept. de 2023
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 comentarios
Bruno Luong
Bruno Luong el 29 de Sept. de 2023
"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 el 3 de Oct. de 2023
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

Iniciar sesión para comentar.

Categorías

Más información sobre Desktop en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by