Issue with nume1 in MATLAB - matlab

Here's a copy-paste of what MATLAB is doing after I type nume1(A) into the command window:
nume1(A)
%Undefined function 'nume1' for input arguments of type 'double'.
Did you mean:
numel(A)
ans =
1034289
Does anyone know how to stop matlab from doing this? This is preventing me from using nume1 in the editor...

Let's assume this isn't a typo and you didn't mean to invoke the numel function.
Make sure you're not trying to call an M-script as a function. The first line in your nume1.m file should read something like
function argout = nume1(argin)
You should make sure your function is either on your MATLAB search path (type path in the Command Window to see what MATLAB's current search path is), or in your current working directory (type pwd).
To see which function or script MATLAB will use, if any, type:
>> which nume1 -all
The top most is the one that will be invoked.

Related

Error message "using openfig" with Matlab 2018b and Octave

I am trying to run a teaching tutorial on Fourier space in MRI image generation, and consistently getting the following error:
Error using openfig
Too many input arguments.
Error in k_space_tutorial (line 20)
fig = openfig(mfilename, 'reuse')
I have made sure that the folder with the code and related files is in the directory:
>> isdir('k_Space_tutorial_David_Moratal')
ans =
logical
1
and in the path:
>> path
MATLABPATH
C:\Users\Myname\Documents\MATLAB
These are the contents of the directory:
>> dir
. half_fov.m openfig.m
.. image_test.bmp rectangular_matrix.m
README.txt imatge_i_espai_k_originals.m replay_pid10644.log
add_awgnoise.m k_space_tutorial.fig replay_pid11732.log
filtre_pas_alt.m k_space_tutorial.m replay_pid13344.log
filtre_pas_baix.m modaldlg.fig
half_fourier_fe.m modaldlg.m
half_fourier_pe.m motion_artifacts.m
Results of the debugger:
openfig is a function that comes with MATLAB, and according to its documentation, the syntax used is correct.
One of the possible reasons for a function being called correctly but leading to an error is that a different function is called inadvertently. This happens when a different function with the same name shadows (hides) the original function. An M-file in the current directory, or in an earlier directory on the path, with the same name will cause this.
Typing which openfig at the MATLAB command prompt will tell you what function is called when that name is used.
In this case, there is an M-file in the current directory with the same name. Deleting this file (or renaming it) will solve the problem.

SPM in matlab: how to call matlab function in Batch Editor

I'm writing a batch using the Batch Editor in SPM8 using matlab. Now I want to include a simple matlab function that I have written myself. I make sure this function is saved in the path of SPM. As input variable the function uses a file from a previous step in the batch, and when I specify the function to be called, it says "Input could not be evaluated".
So now I'm trying to make another easy function work in the SPM batch, for example "sqrt". I put the "Evaluated Input" to 25, the "Type of output variable" to real number, and the "Function to be called" to sqrt. Again there is an error message saying "Input could not be evaluated".
What am I doing wrong here?
Enter the function in single quotes as follows:
'sqrt'
I just tried it and it worked.
Your batchfile should end up looking like this:
matlabbatch{1}.cfg_basicio.run_ops.call_matlab.inputs{1}.evaluated = 25;
matlabbatch{1}.cfg_basicio.run_ops.call_matlab.outputs{1}.strtype.r = true;
matlabbatch{1}.cfg_basicio.run_ops.call_matlab.fun = 'sqrt';

Matlab Error: "Undefined function or method X for input arguments of type 'double'" With Recursion

I'm trying to make Sierpinski triangles with recursion but I get this error:
??? Undefined function or method 'sierpinski' for input arguments of type 'double'.
I understand that it has to do with Matlab not finding the path for my function, but the weird thing is that it can find my main sierpinski(x,y,n)-function but not the same function that I'm trying to call later in order to get recursion.
My code looks something like this:
function sierpinski(x,y,n)
...
sierpinski(x2,y2,n-1)
end
sierpinski([0,1,0.5],[0,0,1],4)
I would be very grateful if someone could help me with this :)
I cannot reproduce the first error you report. It probably has to do with the file not being on the path. The easiest way to avoid this is to change the working directory to the directory that contains the .m file.
The second error you describe in your comment is due to the fact that you're trying to have a file that is a Matlab function and a Matlab script at the same time. Both have the extension .m, but the first contains a function definition (something that can be called with arguments, has local variables, and can return values), and the other one contains a series of matlab statements that are to be executed exactly as if they were entered one by one in the command window.
Do the following:
– Make a Matlab function file sierpinski.m which includes only your function code:
function sierpinski(x,y,n)
hold on
if n == 0
fill(x,y,'r')
else
x2 = [(x(2)-x(1))/2, (x(2)-x(3))/2, x(3)+(x(2)-x(3))/2];
y2 = [y(1), y(3)/2, y(3)/2];
sierpinski(x2, y2, n-1)
end
Save the file to the current directory or a directory on the path.
– In the command window, enter the statement sierpinski([0,1,0.5],[0,0,1],2). The result is a figure window with a skewed red triangle. Not a Sierpinski triangle, but I guess the first step is done. ;-)
Instead of entering that statement in the command window, you can also make a Matlab script file. Edit a file with the name e.g. run_sierpinski.m, which contains the statement:
sierpinski([0,1,0.5],[0,0,1],2)
Again, save the file to the current directory or a directory on the path.
Now you can run the script, either by clicking the "Run" button in the GUI (green triangle or so), or by entering run_sierpinski in the command window. Either way, the result should be the same as entering the statement directly.

Matlab - Can't call function because it says I'm trying to execute a script

In Matlab I defined a function called iReadImage it looks like:
function [outimag] = iReadImage(imaurl)
{code}
I used it for hours and everything seemed to work fine but then I changed one line and all of a sudden it didn't work anymore, even after I deleted that line nothing worked. It always tells me:
Attempt to execute SCRIPT iReadImage as a function:
/home/.../iReadImage.m
When I look at the file it says that it is 0kB....No idea why, I tried kind of everything, copied the function to a new function, rebooted my computer even tried it on other PCs. Two or three times it seemed to work again but never for long until I got the same error message.
Matlab is very particular about how its functions can be constructed. The file functionname.m should start with the first line function [output] = functionname(input). Otherwise, it will assume that it is dealing with a script and not a function. Additionally, if your file is a function, you can declare within it, like:
function y = f(x)
y = g(x) + 2;
function z = g(x)
z = x.^2;
end
end
However, if your file is a script, Matlab does not allow such function declarations. One way to test this would be to trivially turn your existing script into a function (by wrapping it with a function with null input and output), and see if he same error occurs.
The problem is probably that you've changed your working directory (using e.g. cd). You can only run functions that are in the current working directory, or in directories listed in path.
To confirm, type which iReadImage.
My guess is that you have multiple iReadImage files in the directories where matlab searches for scripts and functions. If so it's likley that Matlab have found the wrong one (perhaps one with an error in it?) and tries to execute it.
Make sure you only have one copy of the file (check which directories Matlab searches in with path).
To find out from which directory Matlab will execute your function write which <filename>, that is, in your case which iReadImage and make sure the correct file is used.
You can also use which iReadImage -all to find all iReadImage files.

How to make matlab see functions defined in .m files?

I'm a total newbie to MATLAB but I have to write some code in it. I've had problems with making MATLAB see functions I've defined in external .m files. This is what I've done: I've created a file named, say, foo.m in my home dir with the following contents:
function [y] = foo(x)
% description
y = x + 1
When I run matlab (my home dir is matlab's workdir) it does not see foo function - it replies with standard ??? Undefined function or variable 'foo' message. BUT help foo or which foo return correct data printing help text and pointing on foo.m file respectively.
I must be missing something but I have no idea what it is. This is getting very annoying.
Oh, after several trial and error attempts I've managed to call that function. Unfortunately I can't remember the sequence of steps I've performed. Moreover after restarting matlab it returns to its usual 'Undefined function or variable' response.
I have 7.11.0.584 matlab running on linux.
MATLAB needs to be told which directories to search over to access those m-files. Clearly it cannot be left to search over your entire disk drives. The MATLAB search path is a list of directories that will be searched in specific order to find your functions.
help addpath
help pathtool
You should never put those files anywhere in the official MATLAB toolbox directories. Choose an entirely separate directory.
Finally, be careful not to name your own functions to match the names of existing MATLAB functions. Otherwise, your very next question here will be why your code does not work properly. This is a common cause of strange and confusing bugs.
It seems you're having some trouble with addpath. Try opening the file in the matlab editor and adding a break point in the file. If the file is not on Matlab's path, matlab should ask if you want to change directory or add the file to the path, choose add to the path.
If this doesn't work, try changing the current working directory (displayed in the main window) to the same location as the m file and calling the function. If this doesn't work you're either getting the name wrong ar there's possibly something wrong with your installation.
Occasionally matlab has problems if it does not have write permission to the directory the file's in, so check that too, i.e. make sure admin rights aren't required for the directory or m file.
Oh, and try:
clear functions
to reload all functions into memory.
The function needs to be in MATLAB's path. Use pathtool to tell MATLAB where to find your function. Note that if you name a function the same name as an existing function, MATLAB will use whichever function it finds first according to the order that the paths are listed as you see them in pathtool.
Although coming late but I hope it will help someone.
If in the folder where the function you are calling is residing, there is any other function with the same name as one of the functions from MATLAB toolboxes, then Matlab will not recognize its license and therefore will disable the whole folder from execution, no matter it is properly added to the path. The help will display though.
In order to check it, type:
which name_of_func.m
and you will get the path with "%Has no license available" message.
If it is your own function, you should not get this message but only the path.
Therefore, find the function in this folder which has the same name as a MATLAB toolbox functions, and rename it. I will solve the problem :).
Best Regards
Wajahat