How to keep program data in different m files in matlab - matlab

I want to write a modular matlab program and I have some data structures such as history in my program. Is that true that I have to keep all my data-structures in the main script of my program? In other words if I have some arrays and fields of data, if I put them in other m files, such as functions, they are temporal and they are going to be collected as garbage in my program execution. I am a java developer and now I want to develop some code in matlab.

As Tommaso suggested in a comment, you should use classes. Look at the documentation for classdef to get started. The full documentation to create classes starts at this page.
But to directly answer your question: it is possible to store static data in functions: see persistent.
If you're making a GUI, there are built-in ways to store data, see guidata.
Finally, there is also getappdata and setappdata, which set global variables but specific to one app.
For all that's good in this world, don't use global, it's not worth the hassle, here are plenty of better alternatives.
PS: if the links here break, note that it is always possible to type help <cmd> to get help on one of the functions mentioned here.

Related

MATLAB organise external toolboxes or turn them into packages to prevent shadowing

I'm working on a large data analysis that incorporates lots of different elements and hence I make heavy use of external toolboxes and functions from file exchange and github. Adding them all to the path via startup.m is my current working method but I'm running into problems of shadowing function names across toolboxes. I don't want to manually change function names or turn them into packages, since a) it's a lot of work to check for shadowing and find all function calls and more importantly b) I'm often updating the toolboxes via git. Since I'm not the author all my changes would be lost.
Is there programmatic way of packaging the toolboxes to create their own namespaces? (With as little overhead as possible?)
Thanks for the help
You can achieve this. Basic idea is to make all functions in a private folder and have only one entry point visible. This entry point is the only file seeing the toolbox, and at the same time it sees the toolbox function first regardless of the order in the search path.
toolbox/exampleToolbox.m
function varargout=exampleToolbox(fcn,varargin)
fcn=str2func(fcn);
varargout=cell(1,nargout);
[varargout{:}]=fcn(varargin{:});
end
with toolbox/exampleToolbox/private/foo.m beeing an example function.
Now you can call foo(1,2,3) via exampleToolbox('foo',1,2,3)
The same technique could be used generating a class. This way you could use exampleToolbox.foo(1,2,3)

Matlab GUI : Efficient of global variable Vs handles

Is it more efficient to pass data between functions by global variables than handles structure in Matlab GUI?
Thanks,
Myrick
Just to complement Pariah's and Lucius' answers, if you intend to deploy your GUI as a standalone application you will likely discover that it's a bad idea to use global/persistent variables.
There are of course some workarounds you can use but as Pariah mentioned, it's good practice to limit your use of global variables although they highly simplify code development.
Depends mainly on what you want to do.
I mean normally i do global variables for prototyping, (and it is poor practice to set all variables to global for security and for performance reasons).
But then again when passing variables via handlers, is a good practice that you really should get into because it not only gives you a better insight into the data you are passing but forces you into good coding practices to aid in debugging and future work that you may extend on your current work.
(This is more of a suggestion but also if you set global variables it can create nightmares when debugging code).
depending on how big is your data, you could see differences in terms of speed.
the main reason is (correct me, if I'm wrong):
the handles-struct is always a copy (call-by-value)->big data->low speed; in addition, one has to re-assign changes; global variables are called by reference
but of course, maintaining a code with many global variables can get more complicated. If someone got the time: create a simple performance-test, that compares both variants with different sizes of data passed :)

Where can I find good and simple test functions for evolutionary algorithms?

I've started learning evolutionary algorithms (GA, PSO, ...) and I want to implement them in Matlab and play with different parameters to get a hold of the algorithms' structures and how they work.
My problem is, I don't have some simple test functions to use. For example, functions with multiple peaks/valleys, one global minimum and multiple local ones, .... Nothing complicated, just some simple mathematical functions with their formulas.
I can try to make some up with putting some sin/cos/exp together, but it'll take time and is really frustrating!
Anybody knows of a resource (site, book, ...) that have these listed?
Here is a set from our very own #Rody Oldenhuis:
Test functions
You might want to try those in the BBOB benchmark set. There is also some nice accompanying literature to this set in form of the corresponding GECCO workshop.
Some of the classic functions were mentioned by AGS already and include Rastrigin, Rosenbrock and Generalized Rosenbrock, Schwefel, Sphere, Griewank, etc.. We have also implemented these and more in HeuristicLab, so if you want to experiment you can also try that (PSO and GA are included also).

What was the original reason for MATLAB's one function = one file and why is it still so?

What was the original reason for MATLAB's one (primary) function = one file, and why is it still so, after so many years of development?
What are the advantages of this approach, compared to its disadvantages (people put too many things in functions and scripts, when they should obviously be separated ... resulting in loss of code clarity)?
Matlab's schema of loading one class/function per file seems to match Java's choice in this matter. I am betting that there were other technical reasons for speeding up the parser in when it was introduced the 1980's. This schema was chosen by Java to discourage extremely large files with everything stuffed inside, which has been the primary argument for any language I've seen using one-file class symantics.
However, forcing one class per file semantics doesn't stop mega files -- KPIB is a perfect example of a complicated, horrifically long function/class file (though a quite useful maga file). So the one class file system is a way of trying to make the user aware about code abstraction more than a functionally useful mechanism.
A positive result of the one function/class file system of Matlab is that it's very easy to know what functions are available at a quick glance of a project directory. Additionally many of the names had to be made descriptive enough to differentiate them from other files, so naming as a minor form of documentation is present as a side effect.
In the end I don't think there are strong arguments for or against one file classes as it's usually just a minor semantically change to go from onw to the other (unless your code is in a horribly unorganized state... in which case you should be shamed into fixing it).
EDIT!
I fixed the bad reference to Matlab adopting Java's one class file system -- after more research it appears that both developers adopted this style independently (or rather didn't specify that the other language influenced their decision). This is especially true since Matlab didn't bundle Java until 2000.
I don't think there any advantage. But you can put as many functions as you need in a single file.
For example:
classdef UTILS
methods (Static)
function help
% prints help for all functions
disp(char(methods(mfilename, '-full')));
end
function func_01()
end
function func_02()
end
% ...more functions
end
end
I find it very neat.
>> UTILS.help
obj UTILS
Static func_01
Static func_02
Static help
>> UTILS.func_01()

How to access an array of structs in simulink?

I have the problem, that I have to access a funktion form a dll in matlab/Simulink in the rtw.
This should work with a s function, but I have the needed parameters in a array of structures organized.
The question is now how I can reach them when I want to call my DLL function?
Or is there a better way (e.g. level 2 Matlab files or something similar)?
The pure simulation (without RTW) worked pretty well with level 2 m files but I am not able to write a tlc file for compiling them. I did not find much on the net and the documentation only about C sources.
Thanks
Christian
For signals in Simulink, what you are asking for is an array of buses. There is similar support for using arrays of structs for parameters. For calling an external function, you might want to look at the legacy code tool. You might also be able to use the MATLAB function block to call your external dll.
In addition to what #MikeT says:
Generating code from Level 2 M-S-Functions is problematic. Read this: http://www.mathworks.co.uk/help/toolbox/simulink/sfg/f7-67622.html#brgscav-1
Also, M-S-functions are generally slow, because they run in the MATLAB interpreter: http://blogs.mathworks.com/seth/2010/10/28/tips-for-simulation-performance/
In the end I coded the problem in C and used an array where I defined to order of the elements. Then I wrote some interface functions to access this "virtual" struct.
This is not very good coding but the easiest way I have found and it is portable.
Thanks