Matlab gui files in a package? - matlab

I am trying to put some .fig and their corresponding .m files in package.
I have:
+ui/mainWindow.fig
+ui/mainWindow.m
But when I try to run mainWindow.fig Matlab prints an error from GUIDE:
Error using feval
Undefined function or variable mainWindow
The funny thing is that if i call with its fully qualified name:
ui.mainWindow
the window appears normally (but all callbacks don't work anyway).
I have tried to import ui.* before running it.
Please note that I want to do this as sort of namespace. I don't want to have my entire application in the global scope of Matlab.

Explanation
The fundamental problem is that MATLAB GUIDE is unaware of packages. Normally, it manages the callback names automatically, keeping the .m and .fig files synchronized, and everyone is happy. When the figure is within a package, it fails to properly update the callbacks in the .fig properties - these still point to the unqualified name mainWindow rather than the correct ui.mainWindow. Subsequently, all callbacks fail.
Workarounds
Two ways around this one:
Export your figure: Guide -> File -> Export. Place this file within your +ui folder. Open the file, and do a find-replace replacing all instances of #(hObject,eventdata)mainWindow with #(hObject,eventdata)ui.mainWindow.
Alternatively, you can manually update the references directly within GUIDE itself, without exporting. For each button and element, Right Click -> Property Inspector then edit the 'Callback' field, replacing mainWindow with ui.mainWindow.
Personally, I prefer the first solution because you can replace all occurrences with a single find-replace command.

Related

MATLAB compiler says some functions in my app are using not licensed for compilation functions

I want to compile my app using matlab-compiler it does so, but with issues...
It says there are some functions that are not licensed for compilation.
The problem is that I haven't used those functions (one of them is fimath.m) in my app.
I think these functions are used inside some of my functions which I don't know.
My question is how to find out which one of my functions are using those functions in order to remove them or replace them with other functions.
There are more than 50 functions in my app and it's not possible to check them one by one.
For every returned "unlicensed" function you can execute the following command,
dbstop in <function name> % without the <>
and afterwards run your code normally for several typical inputs/cases. If it stops at one of these breakpoints, look at the call stack (using either dbstack or the Editor tab of the MATLAB GUI), and identify the entry point from your own code.
If none of the breakpoints is ever hit, it could mean that these functions are referred-to inside the code, but some logic is preventing their execution (turning them, practically, to "unreachable code"). In this case, you will likely need to remove these references manually. To know where from, using information from the link posted by VTodorov you can list the dependencies of each file using
[fList,pList] = matlab.codetools.requiredFilesAndProducts('myFun.m');
which can be called on the output of dir (after some minor conversion). It could be useful to use the toponly flag.

No breakpoints after gui figure copying?

Say, I have two following files:
A.m with matlab code that contains function callbacks and
A.fig with corresponding user interface file.
Now, I'm making copy of these two files:
A_copy.m
A_copy.fig
These two files were created by just copying the first ones. Now the questions is, why debugger does not stop inside callbacks? Inside other function debugger stops in its normal way, but not in callbacks. Of course, I've changed function name in A_copy.m from A to A_copy.
Which problem may I face?
Thanks.
Oh my God.
To fix this problem I had to go to main window editor properties and change NAME property. Just a string that contains title of the gui window. Why? Why? Why, Mathworks, WHY?
I can't believe it is made in so stupid way!

hierarchy of functions in MatLab

I have been reading someone else's matlab code and I don't know how the code structured. I mean I would like to know the hierarchy of functions, which function uses which function. I am reading the code to figure that out but its taking a lot of time.
So is there any other way I can see this hierarchy without reading the whole thing? To be honest it is starting to get confusing. Maybe MatLab has a built in function for that! I found this:
How can I generate a list of function dependencies in MATLAB?
but this doesn't seem to be helpful!
The MATLAB profiler will show you what functions are called by your code (and much more information to boot) and allow you to click through the hierarchy of function calls. You can either call profile on and then run your code, then call profile off and profile viewer, or you can simply call profile viewer and type a single line of code to run in the edit box at the top.
Use the dependency report provided in MATLAB:
http://www.mathworks.co.uk/help/matlab/matlab_prog/identify-dependencies.html
There are also some tools on the File Exchange, such as fdep.
No idea about a function to show visible or depended-upon functions. However the basic rules are:
1) Only the first function in a .m file (normally has to have the same name as the file itself) is visible outside that file.
2) Any function can see any top level (see 1.) function if the file is in the matlab path. Matlab can show you the path so you know where it's hunting.
3) The order of the path is important, the first instance of a function called foo found in the path will be called. Obviously the current directory is at the top of the path.
3) All functions in a given file can see all other functions in that file.
That's the basics. No doubt there are other rules, and possibly exceptions to this. But that understanding generally serves me well.
Obviously the easiest way to work out which function is being called is to click on it in the editor and open it.
One thing I do is simply place in each function at the beginning fprintf("inside function <name>/n"); and at the end of the function fprintf("leaving function <name>/n"); where <name> is the name of the function.
This will give you a very specific list of which function is being called by which function (based on the order that they appear). Another thing like this would be to place fprintf("function <name1> calling function <name2>/n"); so you can be more explicit about which function is being called by which one.

MatLab global function issue

I'm learning MatLab & hit a roadblock.
I have an interface.fig file with interface.m which is acting as my 'main' GUI window. From there another file; bright.m is called.
The file bright needs to update global variables in the main file as well as call functions, I have worked out the global variables out but cannot call functions.
Tried everything, looked at doing things like:
reDisplay();
evalin('base','reDisplay()');
interface.reDisplay();
interface>reDisplay();
But had no luck.
Only the first function in an M file is callable from outside of that file. If you want your functions to be globally accessible then you need to save them in independent files.
If you need state to be globally accessible between these functions pass them as arguments or consider using an Object Oriented approach to solving your problem.
If a function in Matlab is defined inside an m-file with file name different than function name - then there is no way of calling this function from outside its m-file.
In order for your reDisplay function to be visible to bright.m, you should have this function in its own m-file called reDisplay.m

What is the closest thing MATLAB has to namespaces?

We have a lot of MATLAB code in my lab. The problem is there's really no way to organize it. Since all the functions have to be in the same folder to be called (or you have to add a bunch of folders to MATLAB's path environment variable), it seems that we're doomed have loads of files in the same folder, all in the global namespace. Is there a better way to organize our files and functions? I really wish there were some sort of module system...
MATLAB has a notion of packages which can be nested and include both classes and functions.
Just make a directory somewhere on your path with a + as the first character, like +mypkg. Then, if there is a class or function in that directory, it may be referred to as mypkg.mything. You can also import from a package using import mypkg.mysubpkg.*.
The one main gotcha about moving a bunch of functions into a package is that functions and classes do not automatically import the package they live in. This means that if you have a bunch of functions in different m-files that call each other, you may have to spend a while dropping imports in or qualifying function calls. Don't forget to put imports into subfunctions that call out as well. More info:
http://www.mathworks.com/help/matlab/matlab_oop/scoping-classes-with-packages.html
I don't see the problem with having to add some folder to Matlab's search path. I have modified startup.m so that it recursively looks for directories in my Matlab startup directory, and adds them to the path (it also runs svn update on everything). This way, if I change the directory structure, Matlab is still going to see all the functions the next time I start it.
Otherwise, you can look into object-oriented code, where you store all the methods in a #objectName folder. However, this may lead to a lot of re-writing code that can be avoided by updating the path (there is even a button add with subfolders if you add the folder to the path from the File menu) and doing a bit of moving code.
EDIT
If you would like to organize your code so that some functions are only visible to the functions that call them directly (and if you don't want to re-write in OOP), you put the calling functions in a directory, and within this directory, you create a subdirectory called private. The functions in there will only be visible to the functions in the parent directory. This is very useful if you have to overload some built-in Matlab functions for a subset of your code.
Another way to organize & reuse code is using matlab's object-oriented features. Each Object is customarily in a folder that begins with an "#" and has the file(s) for that class inside. (though the newer syntax does not require this for a class defined in a single file.) Using private folders inside class folders, matlab even supports private class members. Matlab's new class notation is relatively fully-featured, but even the old syntax is useful.
BTW, my startup.m that examines a well-known location that I do my SVN checkouts into, and adds all of the subfolders onto my path automatically.
The package system is probably the best. I use the class system (#ClassName folder), but I actually write objects. If you're not doing that, it's silly just to write a bunch of static methods. One thing that can be helpful is to put all your matlab code into a folder that isn't on the matlab path. Then you can selectively add just the code you need to the path.
So say you have two projects, stored in "c:\matlabcode\foo" and "c"\matlabcode\bar", that both use common code stored in "c:\matlabcode\common," you might have a function "setupPaths.m" like this:
function setupPaths(projectName)
basedir = fullfile('c:', 'matlabcode');
addpath(genpath(fullfile(basedir, projectName)));
switch (projectName)
case {'foo', 'bar'}
addpath(genpath(fullfile(basedir, 'common')));
end
Of course you could extend this. An obvious extension would be to include a text file in each directory saying what other directories should be added to the path to use the functions in that directory.
Another useful thing if you share code is to set up a "user specific/LabMember" directory structure, where you have different lab members save code they are working on. That way you have access to their code if you need it, but don't get clobbered when they write a function with the same name as one of yours.