renameFields
% renameFields Utility to rename fields in a structure (or character array)
%
% OUTPUT = renameFields(INPUT,OPERATION) modifies the fields in INPUT using
% the requested operation(s) (valid operations listed below). INPUT can be a
% structure, in which case the OUTPUT is a structure whos fields have been
% renamed according to the specified operation(s). Field order is respected
% and retained. INPUT can also be a character array or cell array of
% character arrays, in which case the OUTPUT is a modified array of the same
% size/form as the INPUT, but with the arrays modified according to the
% requested operation(s).
%
% renameFields(STRUCT,OLDNAMES,NEWNAMES) will return a structure where the
% OLDNAMES have been replaced with NEWNAMES. The nth element of OLDNAMES
% corresponds to the nth element of NEWNAMES, which means that OLDNAMES and
% NEWNAMES must have the same number of elements. Calling renameFields with
% three arguments is limited to STRUCT inputs, unlike calling it with two
% arguments, which permits character strings or cell arrays of character
% strings.
%
%
% Example: Make all fields upper-case
% data = struct('variable_one',1,'VariableTwo',2,'variable_Three',3,...
% 'VARIABLE_four',4,'variableFive_',5,'variable6',6,...
% 'variableSEVEN',7,'Variable8and9',[8 9]);
% newData = renameFields(data,'upper');
%
%
% Example: Make all fields use Pascal convention, then trim trailing '_'
% data = struct('variable_one',1,'VariableTwo',2,'variable_Three',3,...
% 'VARIABLE_four',4,'variableFive_',5,'variable6',6,...
% 'variableSEVEN',7,'Variable8and9',[8 9]);
% newData = renameFields(data,{'pascal' 'trim'});
%
%
% Example: Remove spaces/punc., apply underscore and capitalize each word .
% myString = 'You know, a sentence could also be provided… now that would be cool!';
% newString = renameFields(myString,{'underscore' 'no punc' 'first upper'})
%
% Note that the order of operations matters in the above example. Punctuation
% cleansing is done last because having punctuation is useful in identifying
% words/chunks for the other operations.
%
%
% Programmed and Copyright by Paul Shoemaker: www.matlabinvesting.com
Cite As
Paul Shoemaker (2024). renameFields (https://www.mathworks.com/matlabcentral/fileexchange/68349-renamefields), MATLAB Central File Exchange. Retrieved .
MATLAB Release Compatibility
Platform Compatibility
Windows macOS LinuxCategories
- MATLAB > Programming > Functions > Variables >
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Discover Live Editor
Create scripts with code, output, and formatted text in a single executable document.
Version | Published | Release Notes | |
---|---|---|---|
1.0.0 |