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

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';

Related

How to read a variable in set_param SIMULINK function?

I am having a problem reading a value from a file and put it inside Set_param function which will change SIMULINK model parametrs. This is my code where here i get the value of A from a txt file but i want to put A in Set_param.
when the simulink open it shows A not the value of A in the model.
open_system('Transient.slx') %this will open the simulink model
% get a value from txt file and put it in variable A
A= dlmread('C:\xampp\htdocs\RCE\MATLAB\FYP_expirement\SpeedControl\exp_value.txt');
% here when i put the variable A the function does not accept it
set_param('Transient/Gain','Gain','A')
i try A without single quotation also it gives error.
set_param('Transient/Gain','Gain', A)
how i can insert a variable in this function ? or is there any other solution ?
Thank you very much i find a way to make it works. it seems that set_param only accept characters. So after getting the value i should convert it to string like this:
A= dlmread('C:\xampp\htdocs\RCE\MATLAB\FYP_expirement\SpeedControl\exp_value.txt');
s = num2str(A)
set_param('Transient/Gain','Gain', s)
then when i insert s in the function i dont have to use quotation.

Issue with nume1 in 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.

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 invoke a MATLAB script in runtime by its name

Suppose I have 3 m-codes:
code1.m code2.m code3.m
and I want a code for MATLAB to "draw them together" in the sense that when we run the program, we are prompted with, say, "enter code:", then the user types in say "code3" and then code3.m is run.
I am pretty sure there is a simple code to do that, though I can't remember it.
There are two portions to this question, the first of which is getting user input:
Matlab allows you to request user input as shown in this tutorial: http://www.mathworks.com/help/techdoc/ref/input.html
strResponse = input(prompt, 's')
Part two is simply loading the file and executing it, as described by #MetalRain
http://www.mathworks.com/help/techdoc/ref/eval.html
eval(['load code' strResponse '.m'])
Noting that matlab perform string concatanation on the vector for you, so the result for the input of strResponse = 1 is 'load code1.m'
run or eval can do it. You get the name of the file from input.
A (maybe) less flexible but safer method is to use the graphical version of input named inputdlg.