Azzera filtri
Azzera filtri

Inputing all variables in a saved file through a function

68 visualizzazioni (ultimi 30 giorni)
Chris Rose
Chris Rose il 17 Lug 2024 alle 6:23
Risposto: Jatin Singh il 18 Lug 2024 alle 5:29
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 Commenti
Rik
Rik il 17 Lug 2024 alle 6:31
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
Umar il 17 Lug 2024 alle 6:32
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.

Accedi per commentare.

Risposte (1)

Jatin Singh
Jatin Singh il 18 Lug 2024 alle 5:29
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:

Categorie

Scopri di più su Structures in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by