Generate .m-file code for any MATLAB variable

버전 1.1.0.0 (13 KB) 작성자: Volkmar Glauche
Generate .m script code for any MATLAB variable. If run, the code recreates the input variable.
다운로드 수: 3.5K
업데이트 날짜: 2009/6/23

라이선스 보기

Have you ever tried to inspect and modify deeply nested struct/cell variables? Of course, MATLABs variable editor will help to visualise these variables. However, to create such a variable automatically or to change some of the deeply nested fields routinely this submission may be useful.
The main function is GENCODE. This function traverses the input variable recursively. Recursion stops if the value of the current part of the input variable can be expressed as a right hand side of a MATLAB assignment (usually, this is the case for up to 2D matrices, character or cell arrays). In this case, GENCODE_RVALUE is called to create code for the right hand side of the assignment, and GENCODE_SUBSTRUCT is called to create the correct reference into the input variable.

A very simple example:
% struct variable with 2 fields 'a' and 'b'
x.a = 1;
x.b = 'somestring';
% generate code for it
str = gencode(x);
% display this code
char(str)
% clear original variable
clear x
% recreate it by evaluating the code
eval(sprintf('%s', str{:}))

A more realistic example is provided in hgsave_pre2008a.m. In MATLAB 2008a, GUIDE saves %automatic uicontrol callbacks as anonymous function handles. Unfortunately, MATLAB R14SP3 crashes when loading .fig/.mat files that contain certain anonymous functions. One possible workaround is to
1. open the .fig file in MATLAB 2008a
2. generate code for it
3. run the generated code in MATLAB R14SP3
4. save the resulting variable in a new .fig file

인용 양식

Volkmar Glauche (2024). Generate .m-file code for any MATLAB variable (https://www.mathworks.com/matlabcentral/fileexchange/24447-generate-m-file-code-for-any-matlab-variable), MATLAB Central File Exchange. 검색됨 .

MATLAB 릴리스 호환 정보
개발 환경: R14SP3
모든 릴리스와 호환
플랫폼 호환성
Windows macOS Linux
카테고리
Help CenterMATLAB Answers에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!
버전 게시됨 릴리스 정보
1.1.0.0

Fixed bug in sparse matrix creation.
Updated documentation.

1.0.0.0