How to define a path in matlab script? - matlab

I have a matlab script lets say image_process.m and the function image_process accepts one argument which is the picture.
I want to define a path for the images folder so now I can call image_process path image_name.
I know I can do image_process path/image_name but I need to add the extra argument to my function.

It is quite easy, just use the fullfile function to combine
function foo(imageName,cDir)
fullFileName = fullfile(cDir,imagesName)
% Do something with fullFileName
end
If you are using scripts instead of functions, just convert it to functions.
Scripts are bad practice anyway, because they mess with the global workspace, and cause havoc.
Any script can be converted to function.
Call your function like this:
foo('peppers.png','C:\MySpecialDir')

Related

Variable output to workspace with correct function call in command window-fixed- [duplicate]

How can I create a function with MATLAB so I can call it any where in my code?
I'm new to MATLAB so I will write a PHP example of the code I want to write in MATLAB!
Function newmatlab(n){
n=n+1;
return n;
}
array=array('1','2','3','4');
foreach($array as $x){
$result[]=newmatlab($x);
}
print_f($result);
So in nutshell, I need to loop an array and apply a function to each item in this array.
Can some one show me the above function written in MATLAB so I can understand better?
Note: I need this because I wrote a code that analyzes a video file and then plots data on a graph. I then and save this graph into Excel and jpg. My problem is that I have more than 200 video to analyze, so I need to automate this code to loop inside folders and analyze each *.avi file inside and etc.
As others have said, the documentation covers this pretty thoroughly, but perhaps we can help you understand.
There are a handful of ways that you can define functions in Matlab, but probably the most useful for you to get started is to define one in an m-file. I'll use your example code. You can do this by creating a file called newmatlab.m in your project's directory that looks something like this
% newmatlab.m
function result = newmatlab(array)
result = array + 1
Note that the function has the same name as the file and that there is no explicit return statement - it figures that out by what you've named the output parameter(s) (result in this case).
Then, in the same directory, you can create a script (or another function) that calls your newmatlab function by that name:
% main.m (or whatever)
a = [1 2 3 4];
b = newmatlab(a)
That's it! This is a simplified explanation, but hopefully enough to get you started and then the documentation can help more.
PS: There's no "include" in Matlab; any functions that are defined in m-files in the current path are visible. You can find out what's in the path by using the path command. Roughly, it's going to consist of
Matlab's own directory
The MATLAB subdirectory of your Documents directory
The current working directory

clearing many MATLAB functions in one single file

I have many small functions in matlab, is it possible to put them all in one file so my work will look clearer?
i tried writing a small script and then adding the functions but is didn't work
any idea on how do do it?
It is not possible to have several functions in one file, because the function is accessed via the file name. That is why a function has to have the same name as the file name.
If you only access the "small" functions inside one other function, then you can put the small functions in the file of the other function, but then they are only accessible to this one function. This is called local functions in MATLAB. For example you have a file test.m with:
function x=test(y,z)
x = add(y,z)
end
function a=add(b,c)
a = b + c;
end
You can then only use add inside test, but you can use test just as usual.
What I usually do is put functions in subfolders. This helps you keep your path clean without any limitations. This also allows you to capsule your software better. The only thing you have to do is add the folder to your path with
addpath('subfolder');
If you have a function file, you can add other functions in that file.
If you have a script, you cannot add functions to it.
Note that if you put a function in a file, you cannot access the functions directly from outside your 'main' function scope.
In general I would recommend the use of folders, or proper file names to organize your functions, not stacking many of them in one file.
Extra
If your functions are really small and trivial, you could write a script with the declaration of anonymous functions for easy reuse. However this is probably not what you want.

Call a Matlab function not in working directory

Is it possible to have Matlab call a function that is not in the working folder?
For example, can I specify a path like .\bar\foo(arg1) and have the function execute? I have a number of helper functions that I want to use without clogging up my working directory.
Yes, you can use the command addpath to add the folders you need to the MATLAB path.

Suppressing a function's command window output

A function I'm using has display() in it (or other things that display messages on command window), so it outputs a lot of things (x 1200) on command line when I run my code, which makes things hard to track and observe.
Is there a way to suppress the output of this specific function? Ending the statement with semicolon obviously doesn't help.
You might try wrapping the call to the function in an evalc:
evalc('out = func(arg1, arg2);');
The easiest way is to just create a dummy function DISP/DISPLAY and place it in a private folder along with your own function:
private/disp.m
function disp(x)
return
end
myFunc.m
function myFunc()
%# ...
disp(1)
end
By placing the disp function inside a private folder, you override the built-in function with the same name, yet this version is only visible to functions in the parent directory, thus maintaining the original functionality in other places.
Make sure that you DON'T add this private folder to your path, just have myFunc.m on the path (Please read the relevant documentations)

How do I emulate 'include' behaviour in MATLAB?

In MATLAB I can define multiple functions in one file, with only the first defined function being visible external to that file. Alternatively, I can put each function in its own file and make them all globally visible through the path. I'm writing a menu driven application, where each menu item runs a different function. Currently, these are all in one big file, which is getting increasingly difficult to navigate. What I'd like to do is put groups of related functions into separate files.
I think I can do something like this by putting all the child functions into a separate directory and then adding the directory to the path in my parent function, but this feels a bit messy and inelegant.
Can anyone make a better suggestion?
Note: I'm most familiar with MATLAB 2006, but I'm in the process of upgrading to MATLAB 2009.
One suggestion, which would avoid having to modify the MATLAB path, is to use a private function directory. For example:
Let's say you have a function called test.m in the directory \MATLAB\temp\ (which is already on the MATLAB path). If there are local functions in test.m that you want to place in their own m-files, and you only want test.m to have access to them, you would first create a subdirectory in \MATLAB\temp\ called private. Then, put the individual local function m-files from test.m in this private subdirectory.
The private subdirectory doesn't need to be added to the MATLAB path (in fact, it shouldn't be added to the path for things to work properly). Only the file test.m and other m-files in the directory immediately above the private subdirectory have access to the functions it contains. Using private functions, you can effectively emulate the behavior of local functions (i.e. limited scope, function overloading, etc.) without having to put all the functions in the same m-file (which can get very big for some applications).
Maybe something like this,
function foobar
addpath C:\Include\ModuleX
%% Script file residing in ModuleX
some_func();
end
Of course, ModuleX will remain in your search path after exiting foobar. If you want to set it to the default path without restarting, then add this line:
path(pathdef)
See ADDPATH for more details.
You can use sub-folders that begin with "+" to separate functions into namespaces.
For example:
Place a function "bar" in the folder "+foo"
function bar()
print('hello world');
This function can be used as:
foo.bar() % prints hello world
More information can be found here:
What is the closest thing MATLAB has to namespaces?