Undefined function or method 'nanvar_base' in Matlab - matlab

Somehow my Matlab does not has the function nanvar_base, needed to use the function ttest2 to calculate the t and p value of two sample data. How do I install that function? Is it missing to anyone else?

I had this same issue, the ttest2 function calling a nanvar function from SPM rather than the MATLAB statistics folder. A simple fix is going into 'change paths' in MATLAB and moving the SPM paths to the bottom of the list.

The function ttest2 is part of the statistics toolbox.
However, when I try to edit it, it does not show any calls to nanvar_base.
Did you perhaps try to edit it yourself, and in the process made a mistake? If this is not the case, try edit ttest2 and see where it is located and show the line with the call to nanvar_base.

This function isn't part of MATLAB or any of its toolboxes/add-ons. It must be custom functions. There is a function call nanvar however, in the Statistics toolbox and also in the Financial toolbox. Is this maybe the one you're after? If so, do you actually have the Statistics Toolbox and/or the Financial Toolbox?

the problem is probably another toolbox (like SPM) getting in the way, with their own 'nanvar' function.
Move SPM to the bottom of the path definition list, or remove it completely.
In general moving SPM have a lot of conflicting functions, so always make sure their path is at the bottom.

I had the same issue when calling the function nanvar, and realised that there was a conflict with a function with the same name in the Fieldtrip toolbox (this Fieldtrip function calls nanvar_base). After removing the Fieldtrip/src folder from my paths, it worked fine.

Related

How to use MATLAB toolbox function which has the same name of a user defined function

I am having a problem with the findpeaks function, this function is in the signal processing toolbox and also the program has another version of it (user defined function). I need to call the on in the signal processing toolbox not the user defined one, also I can't rename the user defined function for many reasons. Can anyone help me in calling the toolbox function.
The precedence order used by MATLAB is described in their help pages. It states that functions in the current folder (9.) are preferred over functions elsewhere in the path (10.). Then, the first appearance of the function in the path is chosen. This allows for a number of possible solutions:
1. cd to folder
A very simple method is simply to change the current workspace directory to the folder of the function you need to call, i.e. cd either to the place where your user-defined function is, or cd to the toolbox path. Note: This is rather inelegant, but probably sometimes the simplest solution.
2. Reorder path
As mentioned, MATLAB choses the first occurence of the function in the path. You can thus re-sort the path variable, so the folder where your user-defined function is, appears last. The path variable can be viewed and manipulated using the path function. Note: Then you can only call the toolbox function. Otherwise you'd have to resort the path again.
3. Function handles
If you need to be able to call both functions, it can be useful to create a function handle for both versions. For that, you have to cd into the folders where the functions are defined and create a new handle there:
cd('path/to/userdefined/function')
userFindPeaks = #findpeaks;
cd('path/to/MATLAB/installation/toolbox/signal/signal')
toolboxFindPeaks = #findpeaks;
You can then call the functions using feval.
Of course, as Adriaan mentions in the comments, it is best not to use the names of already defined functions for your own functions or for variable names.
I just came here looking for the same thing... I ended up using builtin.
https://uk.mathworks.com/help/matlab/ref/builtin.html
[y1,...,yn] = builtin(function,x1,...,xn)
#arr_sea actually posted a link in one of the folded comments which uses this function in a different context.

Function Fields Feature in Matlab

mapminmax is a builtin Matlab function. I am trying to implement something that does autocomplete for functions/subfunctions like this.
I've done a quick search but haven't really come up with what it's called. mapminmax is the only function I know of that implements this feature. It looks like a field of a function (like how a field of a struct).
I've used edit mapminmax to see the insides of Matlab's function but I haven't found anything leading to how this is possible. getParamStructFromArgs looks like it might be able to explain what this is, but it looks like it's related to NNs.
Question: What is this feature called and is there any documentation on this?
Looks like what #hypfco said was right. This "feature" is related to Matlab's package system. I'm sure there's a way to do it by creating a package, but for those who don't want to create such a package there's a simple way of doing this.
If you have a function such as untitled.m, you can create a folder called +untitled in your Matlab directory.
Function's .m file
+Function folder
Then when you do untitled. and press tab in the console, you'll get the following pop-up.
If anyone's got a way to do this inside the .m file, I'll accept that answer instead.

Is it possible to get all functions involved in function2 call on Matlab?

Is there a way to know the functions involved in a demo on Matlab ? I mean starting from a function/Script demo can I have a tree of all involved files/functions on the current demo that has been called by the demo.
Look into depfun.
Quote from the documentation -
depfun Locate dependent functions of program file.
TRACE_LIST = depfun(FUN) returns a cell array of files of the dependent
functions of FUN.
An alternative, possibly more interactive, way to have a look at what has been used is to use the profiler:
profile on -history
% now run some code or function
profile off
profile viewer
not really what it was designed for but could be used in this way

hierarchy of functions in MatLab

I have been reading someone else's matlab code and I don't know how the code structured. I mean I would like to know the hierarchy of functions, which function uses which function. I am reading the code to figure that out but its taking a lot of time.
So is there any other way I can see this hierarchy without reading the whole thing? To be honest it is starting to get confusing. Maybe MatLab has a built in function for that! I found this:
How can I generate a list of function dependencies in MATLAB?
but this doesn't seem to be helpful!
The MATLAB profiler will show you what functions are called by your code (and much more information to boot) and allow you to click through the hierarchy of function calls. You can either call profile on and then run your code, then call profile off and profile viewer, or you can simply call profile viewer and type a single line of code to run in the edit box at the top.
Use the dependency report provided in MATLAB:
http://www.mathworks.co.uk/help/matlab/matlab_prog/identify-dependencies.html
There are also some tools on the File Exchange, such as fdep.
No idea about a function to show visible or depended-upon functions. However the basic rules are:
1) Only the first function in a .m file (normally has to have the same name as the file itself) is visible outside that file.
2) Any function can see any top level (see 1.) function if the file is in the matlab path. Matlab can show you the path so you know where it's hunting.
3) The order of the path is important, the first instance of a function called foo found in the path will be called. Obviously the current directory is at the top of the path.
3) All functions in a given file can see all other functions in that file.
That's the basics. No doubt there are other rules, and possibly exceptions to this. But that understanding generally serves me well.
Obviously the easiest way to work out which function is being called is to click on it in the editor and open it.
One thing I do is simply place in each function at the beginning fprintf("inside function <name>/n"); and at the end of the function fprintf("leaving function <name>/n"); where <name> is the name of the function.
This will give you a very specific list of which function is being called by which function (based on the order that they appear). Another thing like this would be to place fprintf("function <name1> calling function <name2>/n"); so you can be more explicit about which function is being called by which one.

how a function change in a loop with newtonsys command matlab

i want to change function variable at each step in a for loop in matlab.
i take these steps:
i created my mfile function
function [jfun,fun]=air(x,vt,ty,tz,p2,y0)
jfun=[(cos(tz)*cos(ty)*vt/9.81)*(1-exp(-9.81*x(2)/vt)),...
x(1)*cos(tz)*cos(ty)*exp(-9.81*x(1)/vt);
(-vt*sin(tz)*cos(ty)/9.81)*exp(-9.81*x(1)/vt),...
(x(1)*sin(tz)*cos(ty)*+vt)*exp(-9.81*x(1)/vt)];
fun=[(x(1)*cos(ty)*cos(tz)*vt/9.81)*(1-exp(-9.81*x(2)/vt))-p2;...
(vt/9.81)*(x(1)*sin(tz)*cos(ty)+vt)*(1-exp(-9.81*x(2)/vt))-vt*x(2)+y0];
end
and then i used newtonSys command:
ty=rad(-23)
tz=rad(15)
p2=1.8
vt=8.4925
y0=0.2
myfun=#(x)air(x,vt,ty,tz,p2,y0)
x=newtonSys(myfun,[15 5],0.000001,0.000001,500,1)
and matlab answer:
Error in ==> Untitled18 at 7
x=newtonSys(myfun,[15 5],0.000001,0.000001,500,1)
i guess that its because of wrong use of function handle and for my function i have to use another command or i have to use another method for solving 2nonlinear equation.
First, you haven't displayed the entire error. MATLAB should tell you what the actual error is, so unless you tell us that, we won't know what's wrong.
Secondly I don't see newtonSys in my system, or in the online MATLAB documentation. So most likely it is an external program, which most people here might not be familiar with. The closest I found from a quick Google search was this, which is an implementation of Newton's method for approximating the root of a non-linear system of n-equations. Are you using the same file? If so, if you look at the comments, it says that you need to pass the function as a string. So you'd have to pass myfun as 'myfun'.
If not, could you edit your question and add the contents of the function that you're using? Without these, it's near impossible to answer your question.