Pack & Unpack variables to & from structures with enhanced functionality

Version 1.14 (15.4 KB) by Adi Navve
v2struct packs and unpacks variables to and from structures with enhanced functionality.
7.3K Downloads
Updated 20 Nov 2014

View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

v2struct has dual functionality in packing & unpacking variables into structures and vice versa, according to the syntax and inputs.
Function features:
* Pack variables to structure with enhanced field naming
* Pack and update variables in existing structure
* Unpack variables from structure with enhanced variable naming
* Unpack only specific fields in a structure to variables
* Unpack without over writing existing variables in workspace

In addition to the obvious usage, this function could by highly useful for example in working with a function with multiple inputs. Packing variables before the call to the function, and unpacking it in the beginning of the function will make the function call shorter, more readable, and you would not have to worry about arguments order any more. Moreover you could leave the function as it is and you could pass same inputs to multiple functions, each of which will use its designated arguments placed in the structure.

Moreover, the function enables extracting specific fields from a structure according to field names. This enhances the flexibility and ease of use of structures, for example it allows you to pass same inputs as structures to multiple functions, each of which will use its designated
arguments placed in the structure.

Some examples:
Simple pack:
x = zeros(3);
y = 'Testing123';
z = cell(2,3);
fieldNames1 = {'fieldnames','x','y','z'};
fieldNames2 = {'fieldnames','a','b','c'};

>> S = v2struct(x,y,z)
S =
x: [3x3 double]
y: 'Testing123'
z: {2x3 cell}

pack with different field names:
>> S = v2struct(x,y,z, fieldNames2)
S =
a: [3x3 double]
b: 'Testing123'
c: {2x3 cell}

update existing structure with additional field:
>> S.oldField = 'field to be saved for future use'
>> S = v2struct(x2, nameOfStruct2Update, fieldNames3)
S =
oldField: 'field to be saved for future use'
x: [3x3 double]

simple unpack:
S.x = zeros(3);
S.y = 'Testing123';
S.z = cell(2,3);
fieldNames3 = {'y'};
>> v2struct(S)
y
y =
Testing123

More examples, further comments and demo usage are available inside the .m file and the demo functions.

Comments, suggestions, bug reports and any other feedback is welcomed at adi.navve[at]gmail[dot]com

Inspired by the function 'mmv2truct' from the Mastering Matlab Toolbox (available free) by D.C. Hanselman, University of Maine, Orono, ME 04469 4/28/99, 9/29/99, renamed 10/19/99 Mastering MATLAB 5, Prentice Hall, ISBN 0-13-858366-8
available at: http://www.eece.maine.edu/mm/MM6/tbx.html

Cite As

Adi Navve (2024). Pack & Unpack variables to & from structures with enhanced functionality (https://www.mathworks.com/matlabcentral/fileexchange/31532-pack-unpack-variables-to-from-structures-with-enhanced-functionality), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2011a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Categories
Find more on Structures in Help Center and MATLAB Answers
Communities

Community Treasure Hunt

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

Start Hunting!

html/

Version Published Release Notes
1.14

Made submission availavble as a toolbox

1.13.0.0

Added easy packing of all variables in caller workspace (thanks to Vesa Lehtinen for the suggestion), fixed bug in warning handling in packing case, edited comments.

1.11.0.0

Clarified warning and error when packing/unpacking with fieldNames.

1.8.0.0

fixed bug in packing with variables only

1.7.0.0

fieldNames usage corrected, now for triggering a specific string must be included. Documentation enhanced. Code tweaked.

1.5.0.0

* Added functionality: avoid overwritring existing variables
* Added 'unpacking' examples to demo1 .m file.

1.4.0.0

added update structure functionality, made some documentation and demo function changes, added image :)

1.0.0.0