MATLAB IDE: F1-key and doc function displays help for the wrong function - matlab

I've been working for some time on a MATLAB function to retrieve file names. The function is called getFileName. My problem is that when I try to display help for this function by pressing the F1 key while cursor is on the function name, I instead, get the help for the built in function matlab.io.hdf4.sd.getFilename. I get the same if I write doc getFileName in my command window. Only if I type helpwin getFileName do I get the correct documentation displayed!
This doesn't make sense to me since MATLAB is case sensitive and thereby getFileName is different from getFilename. Furthermore, when I type which getFileName (or for some strange reason, if I type which getFilename), I get the path to my function and not to the built-in function matlab.io.hdf4.sd.getFilename.
So my question is: is it possible to make sure that the function you get documentation for (by pressing the F1 key) is the same function that you run if you type the name of that function?

Matlab isn't actually case sensitive for the help files. In the terminal you can type
doc PLOT
and it will still pop up the documentation for the proper plot function.
I don't know where these files are stored on PC, as I use a Mac, but in the Matlab directory if you search for an uncommon file name (like plotyy) you will see the source file, but you'll also find an html file that doc uses. If you write an html file for the new file it should bring up the right info for the documentation center.
Matlab used to just copy the commented text at the beginning of the file into the documentation center in older versions of matlab, but now it uses html files stored on your drive. I don't know if this will definitely fix your problem as I haven't written an html file for an "almost overloaded" function.
Also, about the weird 'which' thing, I'm pretty sure Matlab first searches the first entry on your path list for a close match, and the current directory is on the top of the list. If you type 'path' into the console it will output all the search paths, and the top ones are searched first.
I apologize for masquerading as if this is an 'Answer' but I don't have enough reputation points to add this as a comment.

Related

Calling user defined function anywhere? (matlab)

Where can i save my document "function.m", so i can call this function from any code in any path of my pc?
I already made some custom functions, but i dont know where to save those codes to be able to call them from any part.
Save them in a logical place where you can find them back. Then, use addpath to add that folder to MATLAB's search path, i.e. when you use a function it will try that folder as well to find it.
You can do this either per script, or edit startup.m with this, so that it's automatically included when MATLAB starts.
Alternatively to Adriaan's best practice answer, the fastest option is to save your function.m in the %USERPROFILE%/Documents/MATLAB directory on your PC ($home/Documents/MATLAB on Linux and Mac). This directory is on your MATLAB Search Path by default. This might be an acceptable solution if you use the function commonly across different projects.
Finally, there is a "manual" solution: Execute pathtool in MATLAB, add the directory containing your function.m via the GUI and hit save.

Copy name of object in variable explorer

I frequently find myself examining deeply nested data in the variable explorer, e.g.:
objectName.structArray1(5).structArray2(3).structArray3(7).doubleArray(4)
In order to be descriptive, the variable names are often long. I often want to use some of the data I'm looking at in Matlab expression, composed at the command line. So I end up typing the lengthy series of variable names and indexes. Autocompletion helps, but not much, especially since my variable names share many substrings.
It would be a lifesaver if I could copy into the clipboard the entire expression corresponding to the data being examined in the variable viewer. I haven't yet found a way to do this (the most obvious way being to right-click the tab for the data being examined). So I'm not sure if this functionality exists. Can anyone confirm or deny (hopefully the former) whether this functionality exists? If it does, how is it done?
As an example, suppose you had a class file myClass.m in the current working directory:
% myClass.m
%----------
classdef myClass
properties
structArray1
end % properties
end % class
Now suppose you issued the following commands:
objectName = myClass
objectName.structArray1(5).structArray2(3).structArray3(7).doubleArray(1:3)=rand(1,3)
openvar('objectName.structArray1(5).structArray2(3).structArray3(7).doubleArray')
You are now examining a slew of data within a deeply nested data structure. Normally, the data would have been the result of computation other than the rand statement above, and I would have browsed to it manually rather than using the openvar statement above. So I would not normally have readily available the text for the expression
objectName.structArray1(5).structArray2(3).structArray3(7).doubleArray
I have to manually type it in at the command line if I want to use it in a Matlab expression for further computation. It'd be so great if I could somehow point to the tab for that data in the variable explorer and somehow have the expression for the data copied to the clipboard. That way, I can paste it to the command line.
AFTERNOTE:
If there's no way to do this, then as an alternative to manually typing in the whole expression above, is there a way to access the corresponding data object (or a copy thereof) programmatically through the variable explorer window object? This assumes, of course, that the variable explorer is itself a data object as well, through which properties can be accessed. If so, maybe it has a property (perhaps deeply nested) that represents the expression for data in the tab that currently has the focus. If so, I can write a function to retrieve the corresponding data object.
I found that if I undock a tab from the Variables editor, I can select the variable name by double-clicking the variable name in the tab.
Sorry for the ambiguity in wording, but "tab" use to mean the protrusion in the data sheet for displaying the name of the data sheet. Nowadays, "tab" means the whole data sheet. In the first sentence above, I mean the protrusion, which unfortunately doesn't have a distinct name these days (at least none of which I'm aware).
After copying and pasting the variable name from the protrusion, the tab can be docked, which seems to put it back into its original place.

MATLAB GUIDE GUI errors following function name change

I have renamed a .fig and associated .m file generated by MATLAB's GUIDE. Having done this, I receive a long list of error messages including the following (just a sample of them shown below):
Undefined function or variable 'my_gui'.
Error in #(hObject,eventdata)my_gui('edit34_CreateFcn',hObject,eventdata,guidata(hObject))
Undefined function or variable 'my_gui'.
Error in #(hObject,eventdata)my_gui('edit33_CreateFcn',hObject,eventdata,guidata(hObject))
Undefined function or variable 'my_gui'.
Error in #(hObject,eventdata)my_gui('edit32_CreateFcn',hObject,eventdata,guidata(hObject))
These errors all seem to relate to individual items on my GUI, such as buttons, text boxes etc.
The function and .fig file used to be called my_gui.m and my_gui.fig. However, I have sinced changed the name to my_new_gui.m and my_new_gui.fig (I've simplified the actual names for the purposes of this question).
So, the obvious solution is to go into the .m file and replace all instances of my_gui with my_new_gui. However, I've done this, and the same error message appears. I have no idea where MATLAB is the reading text my_gui from, since it doesn't exist in any of my code... Any help would be appreciated!
Edit I've discovered that these old references are written in the callbacks for each item on the GUI, which I can change by opening the Property Inspector for each individual item. However, I have a lot of items. If anyone can offer a solution to quickly edit these using a text editor, rather than clicking each individual one, I'd appreciate it!
Renaming MATLAB GUI should be done using Save As... rather than manually changing the file names. Change the file names back to the original names and change the name using Save As... option in GUIDE. This should automatically rename everything.

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

Matlab: how to find functions on path that use a given function

It sometimes happens that I need to change the behavior of a function. Is there an easy way to find functions (on the matlab path) that use that function?
The way I do this is to look for files that contain the name of the function I'm interested in. Thus, I don't only see who calls my function, but also what signature they use.
In the editor: Edit->Find Files
Start with help deprpt.
EDIT: when I run a dependency report there are 3 checkboxes at the top of the report window. One of these, by default unchecked, is called 'Show parent functions (current folder only)'. That is probably the beginning of what you want but it only works in a single folder.
If you want more than that, I suggest that you try edit deprpt.m.
I would use a tool like grep. I posted a tool called mgrep on the file exchange a few years ago. It can search through entire directories of m-files for any given string, and it searched recursively down into sub-directories. So to find functions in my directories that call fminsearch, I would merely execute this at the command line.
mgrep('fminsearch','.','show','off')
Searching directory(ies)...
/Users/woodchips/Desktop/My_FEX/cylinderfit.m
/Users/woodchips/Desktop/My_FEX/fminspleas.m
/Users/woodchips/Desktop/My_FEX/fminspleas2.m
/Users/woodchips/Desktop/My_FEX/fminspleas3.m
/Users/woodchips/Desktop/My_FEX/fminspleasnnls.m
/Users/woodchips/Desktop/My_FEX/BoundedFSOLVE/fsolvebnd.m
/Users/woodchips/Desktop/My_FEX/FMINSEARCHBND/fminsearchbnd.m
...
you mean like looking for dependencies? that would be tools -> show dependency report