Pack & Unpack variables to & from structures with enhanced functionality

버전 1.14 (15.4 KB) 작성자: Adi Navve
v2struct packs and unpacks variables to and from structures with enhanced functionality.
다운로드 수: 7.4K
업데이트 날짜: 2014/11/20

라이선스 보기

편집자 메모: 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

인용 양식

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. 검색 날짜: .

MATLAB 릴리스 호환 정보
개발 환경: R2011a
모든 릴리스와 호환
플랫폼 호환성
Windows macOS Linux
카테고리
Help CenterMATLAB Answers에서 Structures에 대해 자세히 알아보기
커뮤니티
 Power Electronics Control 커뮤니티에 더 많은 파일이 있습니다

Community Treasure Hunt

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

Start Hunting!

html/

버전 게시됨 릴리스 정보
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