retainfields

by Tim C. Lueth, SG-Lib Toolbox: SolidGeometry 5.6 - CODING/DEVELOP
Introduced first in SolidGeometry 4.6, Creation date: 2019-05-26, Last change: 2025-09-14

retains a list of fields from a struct

Description

retain

See Also: stripfields

Example Illustration

 missing image of retainfields(S,L)

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

Output

Algorithm Steps

  1. The function checks if at least one field name is provided in varargin. If not, it throws an error.
  2. The list of field names to retain is assigned to L.
  3. The function retrieves all field names of the structure S using fieldnames(S) and stores them in fn.
  4. The number of fields in S is determined and stored in m.
  5. A loop iterates over each field name in fn:

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