Dynamic Assign Field Access in MATLAB Structures
24 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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
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
el 28 de Sept. de 2023
Exactly as Voss wrote, the best approach is to simply access the data from the structure.
Respuestas (1)
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)
% this is a dirty command that pops the field values to the workspace
cellfun(@(f) assignin('caller',f,s.(f)), fieldnames(s));
whos
a
b
c
7 comentarios
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
el 3 de Oct. de 2023
s=struct('a',1,'b',2,'c',3);
timeit(@()field_access(s),1)
timeit(@()pop_varaccess(s),1)
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
Ver también
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!