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)
Related
I want to use script file as function because somehow, I need to define methods in script file and it is not possible in script file. However, when I defines script as function and execute it then I lose work space and I can not do further analysis of variables. I do not want to set breakpoints in code. Is there a way to keep work space alive for further analysis.
At the end of the function:
save filename;
evalin('caller','load filename');
I just got an evil solution:
%get all the var-names of the fcn-workspace
myVars=who('*')
for var=myVars'
assignin('base',var{:},eval(var{:}))
end
..this will work, but it is eval :)
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.
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')
Tried Googling, but couldn't find anything.
I have a few files and folders in my current MATLAB folder.
One of those folders is called 'Map' and it has a 'map1.m' file which I want to call from my code in the current MATLAB folder.
In my code, I can't call it like this:
/Map/map1;
but I can do so like this:
cd Map;
map1;
cd ..;
Somehow the above method seems incorrect. Is there a more elegant way to do it?
You can run the file without adding the folder to your path manually, using the run command, which is specifically for such cases. From the documentation:
run is a convenience function that runs scripts that are not currently on the path.
You call your function/script as
run /Map/map1
If you want to run the function/script by merely entering its name and not with the full (or relative) path, then you should add the folder to your path.
As noted by #mutzmatron, you cannot use run to call functions with input/output arguments. So, unless if it's a script/function without input/output arguments, using run will not work and you'll have to add the folder to your path.
EDIT
Just as a matter of good coding practice, and to work in cases where your function has inputs/outputs, adding/removing the folder from your path is the correct way to go. So for your case,
addpath /Map
...
map1;
...
rmpath /Map
The important thing is that your function call is sandwiched between the addpath and rmpath commands. If you have functions of the same name in both folders, then you should sandwich it tighter i.e., a line before and a line after, so as to avoid conflicts.
Just add all those directories to the Matlab path with addpath like gnovice suggests. Then you'll be able to call the functions normally, and they'll be visible to which(), help(), depfun(), and the other Matlab meta-programming commands. You can put the addpath() calls in your startup.m file to have them automatically appear each time you start Matlab.
Changing the path with addpath/map1()/rmpath each time has some drawbacks.
It's a performance hit because you're adding path manipulation to each call.
Functions in different directories won't be able to see each other.
It'll be harder to write and debug functions because the path context in which they execute will change dynamically, and won't be the same as what you see when you're in the editor and the base workspace.
You need additional error handling code to make sure the path is properly restored if the called function errors out.
This won't work with the Matlab Compiler, if you want to deploy this code at some point.
And using run() or cd() yourself is ugly, because relative paths are going to have problems.
If you really want to separate the functions in the subdirectories so they can't "see" each other, you can make those directories namespaces by putting a "+" in front of their names, and then qualify the function calls with the namespace, like Map.map1().
Just to contribute to the path-altering debate...
One way to make it a bit "safer" is to write
% start of my code: create function handles
% to the functions I need:
try
cd Map
map1_func = #map1;
catch mexception
end
cd ..
This tries to preserve the current directory, and you get a handle to the function in a different directory.
Only thing is, this method won't work if map1 relies upon other functions in the Map directory.
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?