MATLAB error when running the program "try.m" - matlab

After creating a MATLAB (version 2010a) file in the editor, I am getting the below mentioned error:
"MATLAB cannot run this file because \toolbox\matlab\lang\try.m shadows it in MATLAB path"
Also with this I get an option to "change folder", "add the path". Even after clicking and working around with this option I keep getting the same message.
I looked up in the matlab manual and tried to change the path. Also I checked my path variable, the place where I am storing my matlab files is already there in the path variable. Please help correcting the error. The name of my file is try.m
The code inside 'try.m' is just:
clear all;

TRY is a reserved word in MATLAB, so you shouldn't use it for your filename. Rename "try.m" to something else and you should be fine. You can do this using the "Save As" from the Editor menu and typing a different name.

Is your file in the file editor also named lang.m? Because Matlab sometimes cannot resolve scope differences between m-files in the current directory and m-files that represent built-in functions. It does not appear to be telling you that your file is not in the path; rather it seems to be saying it cannot distinguish your file from \toolbox\matlab\lang.
Can you provide more details about what your file is, such as posting the code and including the working directory name?

Related

How to load .mat files onto Matlab? Basically what's wrong with my code?

For this project we have been given code, and will be changing some inputs and assumptions. Thus, I already possess the original codes, but just changing all the creator's file paths to match my own computer is yielding me a lot of trouble. The following, and many variations of, continually yield errors.
load \Users\myname\Library\Documents\...
The error is
Error using load
'Unable to read file
\Users\myname\Library\Documents...'.
No such file or directory.
My files are stored in my Documents. Another person in my group on windows has used
load C:\Users\hisname\Desktop\...
Is there something I'm missing in my line, similar to the C drive but on Mac? Is my code just completely wrong, I'm able to load files in R quite easily, but Matlab is posing a huge hurdle. I have no experience with Matlab and have been asked simply to run this code.
On the Mac, path components are separated by /, not \. Thus, you should type
load /Users/myname/Documents/filename.mat
You can use the location bar at the top of the command window to change to the directory where your file is located, and then you can type
load filename
to load filename.mat.
Also, are you sure you have a Documents directory under Library? Why?
To run code from a file called "my_file.m", than just open your Matlab and type run my_file.m. This will run your script in the Command Window.
The load function is used, if you want to load a .mat file. These are normally files, where variables from your workspace are stored.

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.

matlab creating paths to stop copying code

I have created a few general function in MATLAB that I intend to use for a few separate projects. However I do not want to copy the function into each separate project function.
I have created a folder called Misc_Function when I have placed these general functions. I know I can reference this functions explicitly by using the path and function name when trying to call the functions.
I believe you can add a path (in my case 'H:\MyTeam\Matlab\Misc_Function') when MATLAB loads up is that correct and if so how do you do this?
Assuming the above can be done I'm interested to know how MATLAB finds the correct function. In my understanding (guess work) MATLAB has a list of paths that it check trying to find a function with the name specified - is that correct? If so what happens when there are functions with the same name?
MATLAB indeed has its own search path which is a collection of folders that MATLAB will search when you reference a function or class (and a few other things). To see the search path, type path at the MATLAB prompt. From the documentation:
The order of folders on the search path is important. When files with the same name appear in multiple folders on the search path, MATLAB uses the one found in the folder nearest to the top of the search path.
If you have a set of utility functions that you want to make available to your projects, add the folder to the top of the search path with the addpath function, like so
addpath('H:\MyTeam\Matlab\Misc_Function');
You have to do this everytime you start MATLAB. Alternatively, and more conveniently, save the current search path with the savepath command or add the above commands to your startup.m file.
You can check the actual paths where Matlab searches for functions using
path
You will notice, that the most top path (on start up) is a path in your home folder. For Linux this is e.g. /home/$USER/Documents/MATLAB. For Windows it is somewhere in the the c:\Users\%USER%\Documents\Matlab (I think). Placing a file startup.m in this folder allows to add additional paths using
addpath('H:\MyTeam\Matlab\Misc_Function');
or
addpath(genpath('H:\MyTeam\Matlab\Misc_Function'));
on start up of Matlab. The latter (genpath) allows to also add all subdirectories. Simply write a file startup.m and add one of above lines there.
I believe 'addpath' will add the folder to MATLAB path only for the current MATLAB session. To save the updated path for other sessions, you need to execute 'savepath' command.
As mentioned in the previous comments, adding the folder in startup.m is a good idea since it will be added to the path on MATLAB startup.
To answer your question about how MATLAB finds the correct function, MATLAB maintains a list of directories in its path in a file called pathdef.m. Any changes to the path will be written to this file (when you execute 'savepath'). The path variable is initialized with the contents of this file.

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

Tab-completion of filenames as arguments for MATLAB scripts

We all know MATLAB provides tab-completion for filenames used as arguments in MATLAB function like importdata,imread. How do we do that for the functions we create?
EDIT:
Displays the files and folders in the current directory.
Caution: unsupported hack here.
Take a look at the file toolbox\local\TC.xml in your Matlab installation. This appears to contain the argument type mappings for tab completion. (I found this by just grepping the Matlab installation for "imread" in R2009b.)
Adding this line inside the <TC> element will get you tab-completion of file names for each of its arguments.
<binding name="importdata" ctype="FILE"/>
This requires modifying the Matlab installation in place. For some of the other files in toolbox/local, like classpath.txt, you can override them by placing modified copies in the directory you start Matlab from, but this didn't work for TC.xml for me.
There is no supported way to add your functions to the argument Tab completion, but one trick I use is to put a "!" in front of the command so it is treated like a system command. Tab will then complete file paths. Once my MATLAB command is done, I home to the beginning of the line, delete the "!" and press enter.
For Matlab 2016a and above :
The file Tc.xml is not present in Matlab 2016a onwards. It uses a .json (Java Script Object Notation) file to achieve the same. A simple example of this can be as follows.
Suppose you have a Matlab function file called myFunction.m. Furthermore, suppose that this function needs files with extension .ext as input and you want the tab-completion to show all possible input options for this function. Then, write the following content in a file and name it functionSignatures.json.
{
"myFunction":
{
"inputs":
[
{"name":"filename", "kind":"required", "type":"filepath=*.ext" }
]
}
}
Place this file in the same directory as myFunction.m file. Now, restart Matlab.
What this file does : While typing in the function input, upon pressing tab, you will see a list of files with the extension .ext popping up. If you want all the files to be shown in tab completion popup irrespective of their extension, then replace "type":"filepath=*.ext" with "type":"filepath" in the file functionSignatures.json.
Source : https://www.mathworks.com/matlabcentral/answers/306157-how-to-configure-tab-completion-for-my-function-in-matlab-2016#answer_237857