MATLAB find references to function - matlab

MATLAB has some great tools, among which this dependency listing function stands out. I'm wondering, is there a way to perform the inverse operation?
That is, fList = matlab.codetools.requiredFilesAndProducts(files) takes a function or script and returns a list of all of the dependencies. I am trying to do the opposite: given a function, I want to find all the functions where this function is called, perhaps limited to the scope of my working directory.
The only solution I can think of is a brute force approach (which would be painfully slow given the speed of matlab.codetools.requiredFilesAndProducts). In MATLAB-esque pseudocode:
foi = file of interest
files = empty set of file lists
i = 0;
for all files f in dir
files{i} = matlab.codetools.requiredFilesAndProducts(f);
i = i + 1;
end
find indices in files where list contains foi
Surely there must be better way.

The best solution I have found is to use the MATLAB "find files" tool (in the latest versions, it is a button on the editor window). It is actually extremely fast, and you can have it search all the .m files in a directory structure, and return every line where a particular string is used - like say, the name of your function.

See if the Parents listing in the dependency report is what you're looking for. It only looks in the current directory, and it has some exclusions.

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

Script for running (testing) another matlab script?

I need to create a matlab mfile that will run another matlab file with default values given in a txt file. It's ment to be useful for testing programs, so that user may specify values in a txt files and instead of inputing values every time he starts the program, my script will give the program default values and user will only see the result.
My idea is to load tested file into a variable, change 'variable=input('...');' for variable = default_variable;, save it to tmp file, execute, and than delete tmp file. Is this going to do the job?
I have only two problems:
1) How to eliminate the problem of duplicated variable names - i mean this must work for all scripts, i don't know the names of variables used in tested script.
2) As I wrote before - is this going to work fine? Or maybe I missed a easier way to do it - for example maybe I don't have to create a tmp file?
I really need your help!
Thanks in advance!
If the person who has to edit the default values has access to Matlab, I would recommend saveing the values in a mat file and loading them when needed. Otherwise you could just write a smalls cript that contains the assignment to certain variables, but make sure to keep this small. For example:
maxRuns = 100;
clusters = 12;
So much for setting up the defaults. Regarding the process my main advice is to wrap the thing that you want to test into a function. This way variables used in the code to call the 'script' will not interfere as a function gets its own separate workspace. Check doc function if you are not familiar with them.

Path matching Matlab

I want to create a function that finds a specific path in Matlab.
The issue is that the path is variable depending on the versions of my program I am working, so
..../...../v1.1/file.m
or
.../...../v1.2/file.m
I would like to know if there is a function to use for hte variable name. Also if the path is too long and I do not want to write it all, is there a symbol that replaces all the previos part. I mean:
strfind(path,$/v1.1/file.m);
But I am not sure of it.
I would appreciate some help!
If you are looking in the path for an instance of the version number, v1.X, then you should just feed it too regexp.
With regard to storing the root of the path and combining it with the version specific portion, I usually use fullfile which handles the path separator for you and makes your code system independent. Finally, in order to handle the version numbering I use sprintf. A lot of people in my lab prefer to use array concatenation, but I find code like that harder to read.
root = matlabroot; % Just an example of a root
version = 1; % Make this a variable in case of future upgrades
subversion = 1; % The actual part from the question
fullPth = fullfile( root, sprintf('v%i.%i', version, subversion), 'file1' );
Do you want to do something similar to this?
versionOfProgram = 'v1.2';
f = fullfile('C:', 'Applications', 'matlab', versionOfProgram, 'file.m');

Matlab: How to know the name of the file are you using in the workspace?

sometimes I load a .m file to work in the workspace, but I usually forget what is the recent file I´ve opened. So in the same way that you write who and you can see the variables of the workspace I suppose there should be a command to know what is the .m file in which you are working.
Does anyone know a command like this?
Thank you so much.
For newer MATLAB versions, there is a way to get the fullpath of the file currently being edited in the Editor:
if verLessThan('matlab', '7.10')
%# not supported
fname = '';
elseif verLessThan('matlab', '7.12')
%# R2010a,R2010b: editorservices
fname = editorservices.getActiveFilename;
else
%# R2011a: matlab.desktop.editor API
fname = matlab.desktop.editor.getActiveFilename;
end
mfilename seemed to be the right choice, but it returns an empty string when called from the command line. Therefore you may check the 'command history' provided by the IDE.
For larger projects it most often makes sense to use MATLAB's object model or at least functions in order to structure your work. Working in the 'workspace' often results in unwanted side effects.
I do something like this to bootstrap runs:
%%
params.run = 'hairy.m';
params.hair = 10;
setup_defaults(params);
run_lots_of_code(params);
This has advantages as it hides the 'globals' in a single global (params), and the global tells you where they came from.

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