i could create my very own precompiler where i fopen the current mfilename and replace every == for the strcmpi, but that sounds like a lot of work for a small problem.
overwriting built in function
    12 visualizzazioni (ultimi 30 giorni)
  
       Mostra commenti meno recenti
    
    Wesley Ooms
      
 il 12 Dic 2014
  
    
    
    
    
    Modificato: Sean de Wolski
      
      
 il 12 Dic 2014
            if i make a conditional statement where i compare 2 stings, i need to do this:
 if strcmpi(string_1,string_2), 
 ...
 end
but the following looks much more elegant, and readable:
 if string_1 == string_2
 ...
 end
that is why i wrote the function
 function y = eq(a,b)
 y=strcmpi(a,b);
 end
but this only works when i put the string in a cell like this:
 if {string_1} == {string_2}
 ...
 end
does anyone know why/ how to overrule the built-inn? also, why does an anonymous function ( eq=@(a,b)strcmpi(a,b); ) not work?
Risposta accettata
  Matt J
      
      
 il 12 Dic 2014
        
      Modificato: Matt J
      
      
 il 12 Dic 2014
  
      You can put your eq.m in a directory @char/. Its parent directory (not its contents) must be on the MATLAB path and higher up on the path than the builtin char methods.
However, I think it is a bad idea to overwrite builtin methods. You don't know what MathWorks-provided mfunctions might rely on the original str1==str2 definition, which you would be breaking.
1 Commento
Più risposte (2)
  Sean de Wolski
      
      
 il 12 Dic 2014
        
      Modificato: Sean de Wolski
      
      
 il 12 Dic 2014
  
      I would strongly recommend against this for a few additional reasons to what have already been provided.
- Other MATLAB functions require the current syntax so basically anything you do with the product is untested by MathWorks.
 - If you give your code to someone else, it won't work for them, and your motivation about "looks elegant" is not enough for most users to want to break their software.
 - I imagine the performance will be significantly slower.
 - Debugging the errors that you get from this can be nearly impossible since a p-file might be expecting the above syntax and getting incorrect results.
 - People with MATLAB experience can look at the code and have a good idea of what is going on. If the definitions of builtin functions change out from under them, this no longer holds true.
 
This is the kind of thing I see done when you want to prank someone temporarily on a short business week. "Hey you called strcmpi!" not something to ever be done for something serious.
My $0.02, not those of my employer.
0 Commenti
  Guillaume
      
      
 il 12 Dic 2014
        You can't do what you want. The function you want to overload is @char\eq which is built-in (hence unmodifiable) according to
which eq -all
And it is a good thing as well. What you want to do is very wrong on two levels:
- It corrupts the meaning of == which means compare the two matrices elementwise and returns which elements differ and which are the same. This applies to any matrix, including matrices of characters
 - strcmpi is a case insensitive comparison. Many people would not associate that with equality. At the very least, strcmp, a case sensitive comparison, would make more sense.
 
The reason your eq works when you wrap the strings into a cell, is because eq is not defined for cell arrays. However, your function will now be called for cell arrays of anything including numbers which makes no sense:
{45} == {45}  %call your function, return 0 for some reason
{'abcd', 'efgh'} == {'ijkl'} %call your function, return [0 0]
{'aa', 'bb', 'cc'} == {'dd', 'ee'} %call you function, error in strcmpi
So don't do this, == has a different meaning from strcmpi, strcmp, strncmp and strncmpi. They're not interchangeable, so use the right one in the right situation.
Also remember that somebody else may have to read / debug your code. If you start changing the rules of the language, they stand no chance of understanding it.
0 Commenti
Vedere anche
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!