Inputing all variables in a saved file through a function
1 次查看(过去 30 天)
显示 更早的评论
I'm sure this question has been asked before, so forgive me, but search results point me towards more basic solutions that don't seem pertinet.
I have a saved file storing a series of structure variables, all with uniform fields, that I would like to process sequentially through the same function.
When loading the file into the workspace I am able to successfully run the function on each individual structure/variable, but struggling to automate this to run sequentially on the whole set.
I've tried findings solutions via structfun and arrayfun implementations to no avail; very possible that my poor familiarity with @(var) syntax is to blame. The function requires an input(one of the structure variables stored within the file), and the consistent roadblock I seem to hit is that structfun/arrayfun is not sending this as a function input so the function sends an error as if I called it with no input.
Any advice is appreciated, even just linking me to a perviously answered questions that address the same issue. Thanks!
2 个评论
Rik
2024-7-17
It sound like you could load the file to a struct, and then use a loop to process each variable.
Can you post your code, or at least a condensed version of it?
Umar
2024-7-17
Hi Chris,
The simple solution to your problem is to automate the sequential processing of structure variables stored in a file and apply a function to each variable by using a combination of load, fieldnames, and a loop to iterate over the variables. For more information on load an fieldnames functions, please refer to
https://www.mathworks.com/help/matlab/ref/load.html
https://www.mathworks.com/help/matlab/ref/fieldnames.html?searchHighlight=fieldnames&s_tid=srchtitle_support_results_1_fieldnames
Please let me know if you have any further questions.
回答(1 个)
Jatin
2024-7-18
Hi Chris,
As per my understanding you want to perform an operation on various fields of structures stored in the saved file.
To help you understand about these functions, “structfun” applies uniform operation on each field of a scalar structure whereas “arrayfun” applies the operation on each element of the array one by one.
In your case you might have to use combination of “structfun” and “arrayfun” if you want to perform the operation on all fields of the structure, kindly refer this sample code which performs an operation on the array of structures(all fields).
% Create an array of structures with multiple fields
people(1).age = 30;
people(1).height = 180;
people(1).weight = 75;
people(2).age = 25;
people(2).height = 165;
people(2).weight = 60;
people(3).age = 28;
people(3).height = 170;
people(3).weight = 68;
% Define the processing function
processField = @(x) 2*x;
% Apply the function to each field of each structure in the array
processedPeople = arrayfun(@(s) structfun(@(x) processField(x), s, 'UniformOutput', false), people);
% Display the processed array of structures
disp(processedPeople);
If you want to perform the operation only on one of the field of the structure then you can also use “setfield” function as illustrated in the below example code.
% Create an array of structures with multiple fields
people(1).age = 30;
people(1).height = 180;
people(1).weight = 75;
people(2).age = 25;
people(2).height = 165;
people(2).weight = 60;
people(3).age = 28;
people(3).height = 170;
people(3).weight = 68;
% Define the processing function
processAge = @(x) x * 2; % Square the numeric value
% Apply the function to the 'age' field of each structure in the array
processedPeople = arrayfun(@(s) setfield(s, 'age', processAge(s.age)), people);
% Display the processed array of structures
disp(processedPeople)
You can also refer to the documents of “arrayfun” and “structfun” with the links given below:
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!