Syntax
S=retainfields(S,[L])
Input Parameter
S : | | struct |
L : | | List of field names as varargin |
Output Parameter
S : | | struct with removed fields |
Examples
S.A=1; S.B=2; S.C=3; S.D=4
stripfields(S,'A','C','B')
retainfields(S,'A','C','B')
Copyright 2019-2025 Tim C. Lueth. All rights reserved. The code is the property of Tim C. Lueth and may not be redistributed or modified without explicit written permission. This software may be used free of charge for academic research and teaching purposes only. Commercial use, redistribution, modification, or reverse engineering is strictly prohibited. Access to source code is restricted and granted only under specific agreements. For licensing inquiries or commercial use, please contact: Tim C. Lueth
Algorithm (Workflow)
This function, retainfields
, is designed to retain a specified list of fields from a given structure in MATLAB. It is part of the SolidGeometry library and was introduced in version 4.6.
Input Parameters
- S: A structure from which fields will be retained.
- L: A list of field names provided as variable arguments (
varargin
) that should be retained in the structure.
Output
- S: The modified structure that only contains the specified fields from the list
L
.
Algorithm Steps
- The function checks if at least one field name is provided in
varargin
. If not, it throws an error.
- The list of field names to retain is assigned to
L
.
- The function retrieves all field names of the structure
S
using fieldnames(S)
and stores them in fn
.
- The number of fields in
S
is determined and stored in m
.
- A loop iterates over each field name in
fn
:
- If a field name is not in the list
L
, it is removed from the structure S
using rmfield
.
Example
Given a structure S
with fields A
, B
, C
, and D
:
S.A = 1; S.B = 2; S.C = 3; S.D = 4;
Calling retainfields(S, 'A', 'C', 'B')
will result in a structure S
that only contains fields A
, B
, and C
.
Algorithm explaination created using ChatGPT on 2025-08-18 23:09. (Please note: No guarantee for the correctness of this explanation)
Last html export of this page out of FM database by TL: 2025-09-21