Inconsistent Find Function output

I have been having some issues with using the find function in Matlab app designer. The gist of my problem is that running my code through Matlab app designer creates a different value then when I run it in a regular script. I have gone through to make sure all the code is correct and I have wittled down the problem to when the find function is used.
My app designer code is this:
dataCar = table2array(cell2table(all'));
dataSorted{k} = (find(agSd{k}==sortrows(dataCar(:,2),'descend'),1))+(find(agDel{k}==sortrows(dataCar(:,3)),1));
my script code is this:
carData = table2array(cell2table(all'));
sortedData{k} = (find(avgSpeed{k}==sortrows(carData(:,2),'descend'),1))+(find(avgDelta{k}==sortrows(carData(:,3)),1));
As you can see they are essentially the same code, but with some variable names changed so I could compare the two. In addition, this is embedded in a for loop hence the "k" as the indexing variable.
The dataCar and carData variables are the exact same array of data and they all have matching values. Also, the agSd and agDel are the exact same values as avgSpeed and avgDelta.
However, with those values being the same, the dataSorted and sortedData cells do not match.
Would there be any reason why, in app designer, the code produces incorrect and different values than the script code?
I have even exported the variables and values from the app designer to my matlab workspace, and I have plugged the dataSorted equation into the command window and I got matching values to the script code. So, I am pretty bewildered and flustered over this.
Any and all information is appreciated!

4 commentaires

John D'Errico
John D'Errico le 21 Juin 2023
Rule #1: NEVER test for exact equality of floating point numbers.
Rule #2: See rule #1.
Indeed.
Although I don't have an explanation of why should/would get different result between code in/out of app designer, it is certainly possible that any change in operations sequence can end up with a differing rounding of floating point values and that will cause a failure in find which is an exact match to the last bit. If the data are read in other than as a .mat file which would preserve the exact bit pattern; there could be the difference or, possibly there's some difference in the optimizer between being inside the app designer versus at command line. Just out of curiosity, have you tried what happens in the compiled version of the app as opposed to interactive?
While perhaps not the location of the changes that cause the behavior, the gyrations in both of
carData = table2array(cell2table(all'));
seem an awfully convoluted way to get to the data in the beginning--why not just dereference the cell array directly instead of moving through two intermediary steps -- or keep a table as the data and get the benefits of extra tools that come along with it?
I'm also having a little trouble in visualizing what it is that is wanted here and that adding the results of two find operations is supposed to return -- oh; I don't think there's anything that says the optimizer couldn't potentially reorder that expression; do you get a failure to find anything or a different set of data are the elements found?
Attach a set of the actual data that illustrate the issue; be interesting to see if such a symptom can be reproduced.
And, the answer to John's rule is ismembertol or writing explicit comparison/lookup expression including an appropriate differential amount.
carData = table2array(cell2table(all'));
BTW, the use of all as a variable is not a good idea; all is a pretty important MATLAB builtin function; aliasing it could have unexpected/unintende consequences.

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Develop Apps Using App Designer dans Centre d'aide et File Exchange

Produits

Version

R2019b

Commenté :

le 22 Juin 2023

Community Treasure Hunt

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

Start Hunting!

Translated by