image thumbnail

Apply a function to a specific field of a structure

by

 

This function searches in a structure, and applies a function to all occurrences of a field.

mypublishtest.m
% test example
%  June 3th, 2010, By Reza Farrahi Moghaddam, Synchromedia Lab, ETS, Montreal, Canada

%% setup

%
clear all;
close all;
clc;

% 
%% example:
sample_struct.check = 5;
sample_struct.level.check = 4;
sample_struct.level.other = 'a';
sample_struct.level.another.test = 7;
sample_struct.level.another.check = -2;

% apply a function to the 'check' fields of the structure
sample_struct = apply_func_on_all_fields_with_name(sample_struct, 'check', @sqrt);

%
sample_struct.check
sample_struct.level
sample_struct.level.another

Contact us