Load Multiple Functions from a Single File in Matlab [duplicate] - matlab

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Is it possible to define more than one function per file in MATLAB?
Is it possible to load multiple functions from the same .m file in Matlab? I find it cumbersome to create a single file for each function for many small alias utility functions. I have already tried this tip which is allowed Octave, but not in my Matlab. I get the following error:
??? Error: File: /home/per/Documents/MATLAB/aliases.m Line: 6 Column: 1
Function definitions are not permitted in this context.
My aliases.m file currently contains
% Prevent Octave from thinking that this
% is a function file:
1;
function y = isvariable(x)
%Return non-zero if x is a function.
y = exist(x, 'var');
end
function y = isfile(x)
%Return non-zero if x is a function.
y = exist(x, 'file');
end
function y = isdir(x)
%Return non-zero if x is a function.
y = exist(x, 'dir');
end
function y = isbuiltin(x)
%Return non-zero if x is a function.
y = exist(x) == 5;
end

I'm afraid that is not possible, each m-file contains exactly one MATLAB function (you can have nested or sub-functions, but they are not accessible outside of the file).
If you are concerned about putting too much stuff on the global scope, think about OOP and namespaces.

Related

why do i get fminsearch undefined function error [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I'm trying to optimize a function with 2 inputs. Trying to use fminsearch but it keeps saying undefined function or variable although it is already defined.
I have already defined the function in a separate script that is in the same directory with my main script. I have a classroom license which includes optimization toolbox and there is no spelling mistake while calling the function.
function o=u(x,y)
%some code here
end
%in a second script
init=[0.1,0.1];
b=fminsearch(o,init);
The error is:
Undefined function or variable 'o'.
From the documentation on fminsearch, the function being minimized must have a single argument and accessed with a function handle (see this related answer).
The error you are getting is because you can't call o and use it as an input to fminsearch() since o is undefined. To get o, you have to first get u(x,y), plus as mentioned, fminsearch requires a function handle as an input.
You have several options that still use your standalone function, u(x,y).
1. Create a function handle
Define a function handle that calls u(x,y) but has a single argument which is a 2 x 1 vector, z = [x; y].
fh =#(z) u(z(1),z(2));
z0 = [1; 2]; % Initial Guess for z = [x; y]
[z,TC] = fminsearch(fh,z0)
2. Change the function and call it directly
The same result is possible with
[z,TC] = fminsearch(#u,z0)
if you redefine u(x,y) to be as follows:
function o=u(z)
x = z(1);
y = z(2);
% your function code here
end

matlab function variable definition [duplicate]

This question already has answers here:
MATLAB not enough input arguments
(2 answers)
Closed 5 years ago.
In Matlab, I want to get the variables from the workspace for the function . But I did not do it.
For example; the function is:
function Y = objfun(x)
Y = 20+x(1).^2 + 2*x(2).^2 -15*x(3);
end
gives me the following problem when I run the function
>> objfun
Not enough input arguments.
Error in objfun (line 5)
Y = 20+x(1).^2 + 2*x(2).^2 -15*x(3);
x variable is exist in workspace like x= [4 5 7] and I don't want to write it inside of function. so what shall I do.
Maybe it is very east question for you but I don't know and I trid make it.
could you help me?
In Matlab (or Octave) you can use scripts or functions.
If you create script called objfun, you have what you are looking for. Just call it using objfun and it will use workspace variable x. The script is saved as objfun.m.
Functions are different. They can have arguments, but these arguments are local variables (only available within the function).
If you define a function, you must call it with the arguments.

Passing two values from one Matlab function to another in one line [duplicate]

This question already has answers here:
How to force MATLAB to return all values in a nested function call?
(4 answers)
Closed 6 years ago.
I'm looking to pass the outputs of a two-output function into a two-input function, in one line.
i.e. if I have two functions
function [out1, out2] = funA(in)
%function definition here
function out = funB(in1, in2)
%function definition here
I want to do something like
out = funB(funA(in)) %this doesn't actually work
Is there syntax to do this without having to write it as
[o1, o2] = funA(in)
out = funB(o1, o2)
I'm also not looking for
[o1, o2] = funA(in); out = funB(o1, o2);
I'm not sure this is possible as if you call the function in-line with another call, Matlab will always assume that you only want the first/primary output.
Matlab only creates the other output variables (out2/in2 here) if you actually assign them.

Access variable in nested parfor loop in MATLAB [duplicate]

This question already has an answer here:
parfor in matlab. sliced variable and nested loop
(1 answer)
Closed 7 years ago.
Consider the following code in MATLAB:
function parallelProblem
N = 10;
A = rand(N);
parfor i=1:N
for k=2:N
A(i,k) = f(A(i,k-1));
end
end
end
function y=f(x)
y = x;
end
This is a summary of the problematic code I'm working on. Basically the idea is the following: I have to variables iand kand I can perform my computation with no communication between different i, but communication between different values of kis required.
Therefore I want to parallelize the loop over i. However, for the code above I get the error
parallelProblem
Error: File: parallelProblem.m Line: 6 Column: 9
The variable A in a parfor cannot be classified.
See Parallel for Loops in MATLAB, "Overview".
Hovering over the word parfor (which is underlined) gives
The PARFOR loop can not run due to the way the variable 'A' is used.
and hovering over f(A(i,k-1)) gives
Valid indices for 'A' are restricted in PARFOR loops
and
In PARFOR loop, variable 'A' is indexed in different ways, potentially causing dependencies between iterations.
From an intuitive point of view, I see no reason why the code should be working in parallel. Is there any way, I can modify my code to get the desired result?
The problem is that you are overwriting A. Loops in a parfor are not executed in order and MATLAB sees that you are overwriting the values of A, and then using them. You can easily fix this using an auxiliary variable:
function parallelProblem
N = 5;
A = magic(N);
Aresult=[];
parfor i=1:N,N
b=[];
for k=2:N
b(k) = f(A(i,k-1));
end
Aresult(i,:)=b;
end
end

Out of memory in Matlab - how to do in-place operation on matrix elements?

I am loading a quite large matrix into Matlab. Loading this matrix already pushes Matlab to its limits - but it fits.
Then I do the following and I get an out-of-memory error.
data( :, 2:2:end, :, : ) = - data( :, 2:2:end, :, : );
Is Matlab allocating a new matrix for this operation? I would assume this operation would not need extra memory. How do I force Matlab to be more efficient for this?
Bonus question:
'data = permute(data,[1 2 3 4 5 12 8 7 6 9 10 11]);'
Can matlab do this in-place?
There are a few constraints (further to those from Loren's block cited by John):
Your code must be running inside a function
You must have no other aliases to 'data'
The 'aliases' thing is both important and potentially hard to get right. MATLAB uses copy-on-write, which means that when you call a function, the argument you pass isn't duplicated immediately, but might be copied if you modify it within the function. For example, consider
x = rand(100);
y = myfcn(x);
% with myfcn.m containing:
function out = myfcn(in)
in(1) = 3;
out = in * 2;
end
In that case, the variable x is passed to myfcn. MATLAB has value semantics, so any modifications to the input argument in must not be seen in the calling workspace. So, the first line of myfcn causes the argument in to become a copy of x, rather than simply an alias to it. Consider what happens with try/catch - this can be an in-place killer, because MATLAB has to be able to preserve values if you error out. In the following:
% consider this function
function myouterfcn()
x = rand(100);
x = myfcn(x);
end
% with myfcn.m containing
function arg = myfcn( arg )
arg = -arg;
end
then, that should get in-place optimisation for x in myouterfcn. But the following can't:
% consider this function
function myouterfcn()
x = rand(100);
x = myfcn(x);
end
% with myfcn.m containing
function arg = myfcn( arg )
try
arg = -arg;
catch E
disp( 'Oops.' );
end
end
Hope some of this info helps...
Matlab does support in place operations. Here's a discussion: http://blogs.mathworks.com/loren/2007/03/22/in-place-operations-on-data/. Sorry I can't be more help.
I don't think there's a good way to know what MATLAB is really doing under the sheets. I would recommend that you:
Make sure you clear any variables that aren't in use before trying the operation.
If you're still running out of memory, write and compile a simple mex file to do the operation in place. With a massive array and your specialized requirement, it'd probably be faster than MATLAB's approach too.
Likewise, you could write your own permutation algorithm as a .mex file in C if you require that it be done in-place (i.e. you're running out of memory again).