How do I tell Matlab to use the proper "range" function?
Afficher commentaires plus anciens
I have a script that I wrote a while ago that started misbehaving after I installed the Antenna Toolbox. This script uses the "range" function to find the largest delta between min and max values in the first column:
myrange = range(myarray,1);
I got an error and then checked the help on this function and found that it's coming from a radio propagation function in that toolbox.
which range
C:\Program Files\MATLAB\R2022a\toolbox\shared\channel\rfprop\@txsite\range.m
How do I get Matlab to use the basic "range" function in my script?
Also, as a followup, I ran:
which range -all
and it only shows the function in that rfprop toolbox. So I don't even know where the original "range" function is supposed to be.
14 commentaires
Fangjun Jiang
le 20 Sep 2022
This range.m you show is private. It will not be called. What is the error message you got?
Andy
le 20 Sep 2022
Fangjun Jiang
le 20 Sep 2022
Notice the "@txsite" in the folder? It means this range() function is a method for the txsite class. Usualy only a txsite object can run this range() method. txsite is for creating RF transmitter site.
Maybe I should not say it is private. Take function isequal() for example, when you run isequal(1,1) and isequal('a','b'), different isequal() functions are called. You can run "which -all isequal" to see there are many isequal() function.
MATLAB is able to call the correct isequal() function based on the data class of the input arguments. If you have both range() function available, most likely it will find the correct one.
Andy
le 20 Sep 2022
Walter Roberson
le 20 Sep 2022
It is too late now, but in future you can track more specifically by using
which range(myarray,1)
MATLAB will analyze the type of myarray and tell you exactly which range function is being invoked.
Fangjun Jiang
le 21 Sep 2022
I would not regard this as a bug. Technically, we should call \stats\range() or txsite.range() based on the use case. MATLAB provides this convenience by adding all functions into the path and being able to call the right function based on the use case. I like this feature. There are so many operations bear the same name, such as open, save, delete a varible, or a file, or an audio, or a video.
The root cause of your problem was that you don't have that function you meant to call. We took the convenience provided by MATLAB for granted.
Andy
le 21 Sep 2022
Walter Roberson
le 21 Sep 2022
When I look in the @txsite code, range() appears to be a hidden method, and is not marked as a static method. So txsite.range() should not ever be invoked unless the parameter was a txsite object (or derived from one.)
It is not possible to add a @ directory to the MATLAB path; addpath() will refuse it. So it is not possible for range() of @txsite to accidentally end up on the path for non-txsite objects. Methods defined in a @directory that are not marked as static can only be invoked by the class.
This is why it would have been useful to which range(myarray,1) while the problem was occuring, to figure out exactly what range was resolving to given those parameters.
Andy
le 21 Sep 2022
Walter Roberson
le 21 Sep 2022
You might be able to use pathtool to remove the stats toolbox from your path. Just do not "save" the resulting path.
Andy
le 29 Sep 2022
Walter Roberson
le 29 Sep 2022
Hmmm, I have no explanation at this time.
Fangjun Jiang
le 30 Sep 2022
Modifié(e) : Fangjun Jiang
le 30 Sep 2022
I noticed this befroe but didn't bring it up. My take is this:
"which -all range" shows all the range.m files in the path. It also shows the range.m files inside a class, which is surprised to me. For example
\toolbox\stats\stats\range.m
\toolbox\fixedpoint\fixedpoint\@embedded\@numerictype\range.m % embedded.numerictype method
\toolbox\mbc\mbcmodels\@xregexportmodel\range.m % xregexportmodel method
\toolbox\mbc\mbcmodels\@xregmodel\range.m % xregmodel method
\toolbox\mbc\mbctools\@sweepset\range.m % sweepset method
\toolbox\shared\channel\rfprop\@txsite\range.m % txsite method
\toolbox\stats\distributed\@distributed\range.m
"which range(1:4)" tries to find a range.m that fits the input argument. If you remove \toolbox\stats\stats from the path, then it won't find any range.m that fits it. So the " 'range(1:4)' not found" message makes sense.
After removing \toolbox\stats\stats from the path, if you run range(1:4), I got the error message in R2022b
"Incorrect number or types of inputs or outputs for function 'range'"
My guess is that it didn't find the right fit, so it ran the first range.m and gave error message regarding incorrect number or types, because it didn't know whether the user didn't intend to use this range.m, or, the user intended to use this range.m but made mistake providing inputs.
In summary:
"which range(1:4)", it can afford to tell you that there is no matching range.m
"range(1:4)", It can't affort to tell you that range.m does not exist. It tells you that range.m does exist, but there is an error running it.
Of course, all this can be changed. It is up to the Mathworks how to present the message.
Réponses (1)
Fangjun Jiang
le 20 Sep 2022
Modifié(e) : Fangjun Jiang
le 20 Sep 2022
0 votes
Run "which -all range".
The first one is used. The rests are shadowed, or private which will not affect you.
The 'the basic "range" function' comes from this toolbox. Make sure you have it.
\toolbox\stats\stats\range.m
7 commentaires
Fangjun Jiang
le 20 Sep 2022
Then you don't have the stats toolbox and you don't have the range function. Your error is probably "unrecognized function or variable range"
Andy
le 20 Sep 2022
Andy
le 20 Sep 2022
Walter Roberson
le 20 Sep 2022
@txsite\range.m can only get called for objects of class txsite, unless range happens to be defined as a static method of class txsite.
Andy
le 20 Sep 2022
Catégories
En savoir plus sur Parametric Modeling dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!