comparevars

version 1.0.0.0 (4.14 KB) by Robert Cumming
Compare any variables to detect difference in class, size and within a user defined tolerance

571 Downloads

Updated 8 Apr 2014

View License

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.

Cite As

Robert Cumming (2022). comparevars (https://www.mathworks.com/matlabcentral/fileexchange/46165-comparevars), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2013b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Acknowledgements

Inspired: comparevars_inf

Community Treasure Hunt

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

Start Hunting!