comparevars

版本 1.0.0.0 (4.1 KB) 作者: Robert Cumming
Compare any variables to detect difference in class, size and within a user defined tolerance
597.0 次下载
更新时间 2014/4/8

查看许可证

Compare any variables to find differences, similar to isequal - but this function will tell you where the differences are in terms of the variable that differs and why.
Comparison can be done with tolerances (default 1e-8) to allow for precision changes.

Excellent for using while developing algorithms to check results before and after: e.g.

before = YourAlgorithm()
% change you code
after = YourAlgorithm();

comparevars ( before, after );

Funcion compares the two inputs (whether they are cells, structs, matrix, etc...) and tell you where any differences are.

Should work with any data class - if you find any that it does not work with please contact me with an example and I will update the code.

Also any feature requests - please let me know and I will try to build into the next release.

Extract from help below:

comparevars compare variables to within a given tolerance

Checks for variable comparison - returns true or false

flag = comparevars ( A, B )

flag = comparevars ( A, B, DCP, NaNCheck )
DCP : number of decimal places to check (1e-8 default)
NaNCheck : if true (default) NaNs are treated as being equal

entering [] in either DCP or NaNCheck will
result in the code using the defaults.

flag = comparevars ( A, B, DCP, NaNCheck, debug )

debug : flag to write check log to screen (default true)

see also isequal isequalwithequalnans test_comparevars

Example

% create a structure with lots of difference data types
A.data.level.x = [1 2 3];
A.data.level(2).x = [1 2 3];
A.data.level(2).func = str2func ( 'plot' );
A.data.cell = cell(20,1);
A.other.log.flag = true;
A.dummyData.integers.test = int8(1);
A.dummyData.char(20).example = 'ABCDeFGH';
A.dummyData.img =i;

% change value x
B = A;
B.data.level(2).x = zeros(3,2,4,3);
flag = comparevars ( A, B )

Check failed on size of variables not the same
A(1).data(1).level(2).x (1,3)
B(1).data(1).level(2).x (3,2,4,3)

ZIP file also contains an example mfile which performs example comparison.

引用格式

Robert Cumming (2024). comparevars (https://www.mathworks.com/matlabcentral/fileexchange/46165-comparevars), MATLAB Central File Exchange. 检索时间: .

MATLAB 版本兼容性
创建方式 R2013b
兼容任何版本
平台兼容性
Windows macOS Linux
类别
Help CenterMATLAB Answers 中查找有关 Workspace Variables and MAT-Files 的更多信息
致谢

启发作品: comparevars_inf

Community Treasure Hunt

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

Start Hunting!
版本 已发布 发行说明
1.0.0.0