File Comment by Comment
07 Dec 2014 Angle Conversion Converts degrees to radians and radians to degrees. Jan Simon

If the input array is large, a tiny modification improves the efficiency:

degrees = radians * (180 / pi);

Without the parentheses Matlab processes the line from left to right :

degrees = (radians * 180) / pi;

For scalars this is no difference except for the rounding errors. But for larger arrays the 2nd version has twice as much operations.

28 Nov 2014 times2(x) Result for Cody Challenge "Times 2" Jan Simon

This is neither a optimal solution nor is the FileExchange the right location to post Cody answers.

21 Nov 2014 FAST, PROGRAMMATIC string searching in directories of MATLAB code files VERY FAST Command-Line searching for text contained in MATLAB code files. Jan Simon

"userpath" is built-in Matlab function. Why is it included in this submission?

01 Nov 2014 WindowAPI Set figure size, top-most, transparency and pixel mask, multi-monitor etc by Windows API Jan Simon

nMonitor = size(get(0, 'MonitorPositions'), 1)

26 Oct 2014 Producing negative image in Matlab Producing negative image of any image Jan Simon

Again: This is a script, but only a function would be useful for others. A loop without a pre-allocation is a really bad programming style, it is hard to implement this worse in Matlab.

Think of vectorization:

I1 = 255 - I;

As you can see, the procedure is trivial. So I do not see a reason to publish this in teh FileExchange.

26 Oct 2014 Flipping image Flipping an image without using function Jan Simon

This is less efficient than FLIPLR and FLIPUD. And even if there would be any reason to avoid these function, prefer the vectorized methods:

B = A(end:-1:1, :);
C = A(:, end:-1:1);

This is much faster than the loop methods, especially when the pre-allocation is forgotten.

This submission is neither useful for educational purposes nor for the productive use. It is a script with a hard coded input, without comments and a really trivial algorithm. It will not be useful for the users of the FileExchange. Therefore I rate it with 1 star only.

26 Oct 2014 Normalizing data this code is for normalizing a matrix N*M Jan Simon

You do not need the image processing toolbox for this script. It is not useful, that you provide hard coded input data and the output is written to an XLS file with a fixed name.

The computations are performed more efficient by this line:

out = bsxfun(@rdivide, in, max(in, [], 1);

12 Oct 2014 fullscreen(h) Display graphs in full screen by simply calling fullscreen(). Jan Simon

The current version fails, if the figure's units are not set to pixels.

12 Oct 2014 fullscreen(h) Display graphs in full screen by simply calling fullscreen(). Jan Simon

Setting the position to 'normalized' [0,0,1,1] works with multi-monitor setups also, when the screen have different sizes.

27 Sep 2014 3D structuring element (sphere) strel3d creates a sphere as a 3D structuring element, which is important for 3D datasets Jan Simon

A simplification:

sw = (sesize-1)/2;
ses2 = ceil(sesize/2);
[y,x,z] = meshgrid(-sw:sw, -sw:sw, -sw:sw);
m = sqrt(x.^2 + y.^2 + z.^2);
b = (m <= m(ses2, ses2, sesize));
se = strel('arbitrary', b);

18 Aug 2014 BlockMean Mean of rectangular submatrices, fast C-Mex (no running mean) Jan Simon

@sykam varma: As far as I understand: Yes. Simply try it.

05 Aug 2014 tprintf -- Print to Second Terminal Window Print lines of text in secondary windows resembling command window. Jan Simon

If you open the figure with 'IntegerHandle' set to 'off', the figure get a unique handle which is guaranteed not to collide with a user defined figure.

An axes appears "magically" in a new figure only, when a plotting command is called. But creating an invisible axes object is not useful to prevent this. So this is a kind of voodoo. ;-)

02 Aug 2014 tprintf -- Print to Second Terminal Window Print lines of text in secondary windows resembling command window. Jan Simon

I do not understand the sense of
fH = min( setdiff(1000:10000,fHs) );
when fH is overwritte later.
fH = findobj(allchild(0), 'flat', 'Name', figureName)

The axes aH is not used anywhere. So why do you create it?

You have the handle of the UICONTROL sH allready. Why do you obtain it by a loop and comparing the type of the figure's children? It would be cleaner to call either FINDOBJ instead of a loop, or to store the handle in teh figure's ApplicationData (see GUIHANDLE).

02 Aug 2014 DateStr2Num Convert date string to date number - C-Mex: much faster than DATENUM Jan Simon

@joh: What exactly is an "array of dates"? Cell strings are handles internally already.

24 Jul 2014 flip Flip vectors and matrices in all directions Jan Simon

Another interesting point is the "linear indexing": x(end:-1:1) accesses all elements even of multi-dimensional arrays.

Please fix the documentation here and in the code: The vector of length N is:
[x(N), x(N-1), ..., x(1)]. It does not start at x(N-1).

The help section has a H1-line, a useful description of the procedure, and a See Also line. The file contains a change log and the name of the author. The actual code is nearly trivial, so I do not think, that this submission is very useful for others. Therefore I rate it with 4 stars instead of 5, although I do not see any point, where it could be improved.

24 Jul 2014 flip Flip vectors and matrices in all directions Jan Simon

The same command works for matrices and multi-dimensional errors also:

x = [1,2; 3,4];
x(end:-1:1) = x;
disp(x)
>> [4,3; 2,1]

This is much faster and simpler than the FOR-loop. In fact, it is such simple that I'd hesitate to create a function for it.

23 Jul 2014 flip Flip vectors and matrices in all directions Jan Simon

A vector of length N has the elements from 1 to N, not to N-1.

What about the much easier code, which considers arrays of all dimensions with a single line:

x(end:-1:1) = x;

15 Jul 2014 mysqueeze.m Squeeze *only* along the SELECTED dimension (not along all of them) Jan Simon

A small simplification: You do not need "Zerones",

Asize = size(A);
Asize(squeezedim) = [];

09 Jul 2014 ScaleTime Fast linear interpolation of equally spaced data (C-MEX and M) Jan Simon

@Andrea: I do not know the meaning of your data and the contents of the variable "value". You could send me a running example to the email address found in the code.

08 Jul 2014 xunits, yunits and zunits.m (Jul 2014) Add unit (and prefix) to your axis labels. Jan Simon

I did not run it yet, butthe code looks really nice an clear. The documentation is useful and considers all of the Matlab standards. The comments in the code are meaningful, such that the user can understand, what is going on.

I would avoid calling an anonymous function in the CASE conditions and prefer hard coding. Then it would become clear, if 'deca' is prefered to 'deci' due to the initial leading 'dec'.

There is a typo in 'mili', which needs a 2nd 'l'. The three functions are almost equal, so it could be worth to combine them to improve the maintainability. E.g. that YUNITS calls XUNITS with an additional input argument to determine the axis.

06 Jul 2014 VChooseK Choose K elements from a vector - MEX: 100 times faster than NCHOOSEK Jan Simon

@Nitin Agrawal: No, there is no pure C-code. I meant, that you can use the MSVC2010 version to compile the C-mex function. Sorry.

29 Jun 2014 VChooseK Choose K elements from a vector - MEX: 100 times faster than NCHOOSEK Jan Simon

@Nitin Agrawal: mex.h belongs to Matlab. If you want to use the function in a C-environment, you neither need mex.h nor the gateway function mexFunction(). Some modifications are required, e.g. the functions for the errors and warnings. I'd suggest to start from the inc-file and implement one data type required for your problem only.

25 Jun 2014 clearempty clear all empty variables from current workspace Jan Simon

If WHOS is used instead of WHO, you can avoid the first evil EVAL. The 2nd one is not required also:
clear(clearEmptyList{clearEmptyk});

Another EVAL and loop-free approach:

whosList_ = whos;
nameList_ = {whosList_([whosList_.bytes]==0).name};
clear(nameList_{:}, 'whosList_', 'nameList_');

20 Jun 2014 XSum Fast Sum with error compensation Jan Simon

@bmv: I've struggled a lot with a string comparison command, which does not depend on the platform.

This tool was developped for experiments. Obviously Knuth and Knuth2 are worth to be applied in productive code, so I think about a multi-threaded version for a fair speed.

I started to write an XCumSum also, but stuck on the decision, if it should offer saturated arithmetics for integer types also.

@Moshe: You find my email address in the help section of the code. The uppercase characters defend spam bots and must be replaced obviously.

20 Jun 2014 MinMaxElem Min and max element and index - C-Mex faster than MIN/MAX Jan Simon

@Jim: It would be useful for me to receive your unit-test output by email. I guess, that the /fp:fast flag impedes the detection of NaN's?

This tool was faster for Matlab 2009a. But modern Matlab versions apply a better MAX algorithm and profit from multi-threading. Therefore only the 'finite' flag is a real benefit of this submission now and I will remove the "speed claims" from the documentation, until I add multi-threading also.

15 Jun 2014 XSum Fast Sum with error compensation Jan Simon

@Moshe: The problems you observe depend on the compiler of the Mac. Please try the new version when it appears in the next days. In case of further problems, send me an email.

14 Jun 2014 Check whether directory is on current MATLAB path Small low-level utility to check whether (list of) directories is/are on the current search path Jan Simon

You define the license as: "GPL + anything implied by placing it on the FEX". But the BSD license required for the FEX and GPL are not compatible. This legal stuff is bewildering.

23 May 2014 Month and Day of month to Day of year (mmdd2ddd.m) Converts month and day of a month to corresponding day of the year Jan Simon

The Financial Toolbox is required for this function.

23 May 2014 Day of year to Month, Day of Month (ddd2mmdd.m) Converts day of a year to corresponding month and day of the month Jan Simon

A function without a documentation is not useful. A help section is obligatory.

A simpler version, which does not need the Financial Toolbox:

function [mm,dd] = ddd2mmdd(year, ddd)
v = datevec(datenum(year, 1, ddd));
mm = v(2); dd = v(3);

17 May 2014 killOrphanFiles() Search all unreachable code files, from one or more Main Entry files, and delete them. Jan Simon

The called functions are recognized by a simple search of the file name in the complete code. Even names appearing in strings or comments are treated as a call.
Although this is mentioned in the help section, it concerns the core job of this function. Therefore this function is of limited use. The nicest graphics are not useful, when the shown contents is not determined sufficiently.

You find several submissions to obtain a list of dependent functions reliably.

15 May 2014 sort1.m sort vector algorithm Jan Simon

@John: Together with your comments, the code is valuable for teaching. You see a working piece of code and the details, how to improve it until it is useful and usable. Therefore I can imagine, that this submission could be expanded to a tutorial including the different versions of the code, a copy of the comments and finally a nice and cute version. Then the "result" is not the final version, but the documentation of the progress of improvement.

Sorting has been discussed too often to be exciting anymore. But a well documented development from bad to good code is rare in the FEX and I would give a high rating. I imagine comments in the code for each modification like better names of variables, more flexible input handling, etc.

10 May 2014 ludecomposition.m ludecomposition Jan Simon

Starting a function with a "clear" command is senseless. You use input arguments "A,b,N", but overwrite A and b by INPUT commands and N by the SIZE command. This is at least confusing.

Defining the input arrays by the INPUT command is cruel. Better use input arguments.

The message 'cannot solve' cannot appear, because the BREAK command in the line before let Matlab jump behind teh FOR loop already. You would detected such bugs, if you test your code.

Omitting the trailing semicolons is a strange idea.

Using "sum" as name of a variable shadows the builtin function with the same name. Btw., SUM() would be more efficient than your loop.

What is the purpose to publish this code? You can find more powerful implementations in the FEX. The absense of a help section and the lean comments reduce the usability.

Currently the published code is neither useful for educational purpose nor for the productive use.

10 May 2014 sort1.m sort vector algorithm Jan Simon

This is definitely not a "standard" sorting algorithm, because it has a bad efficiency. What's wrong with QuickSort or InsertionSort, BubbleSort or QuickerSort? What is the benefit compared to the much faster SORT command?

The "else continue" statement is a waste of time. You can simply omit it.

03 May 2014 num2digit.zip Split a positive number into digits, following the digit order from the original number Jan Simon

A documentation inside the code would be necessary to allow a proper usage.

It is better to let a function stop with an error in case of wrong inputs, than to let it reply an invalid number. If such a function is part of a large program with millions of lines of output, it will be hard to bedug, when just a message is displayed.

Here a faster and more compact method:

out = sprintf('%0*d', len, num) - '0'

01 Apr 2014 Matlab Card Made your own Matlab / ID Card Jan Simon

I do not know, what a "Matlab Card" is. It would be useful to know, what a submission is good for before downloading.

23 Mar 2014 GetFullPath Full path name for partial or relative path Jan Simon

@Charles: Microsoft decided not to support path names with more than 260 characters along time ago. The consequences are cruel, e.g. file with such full path names cannot be thrown to the recycle bin in the Windows Explorer and a drag&drop copy to a network drive is not possible also. A lot of other API functions are affected by this restriction. The C-extension allows to use at least one reliable method of the operating system, which can handle even Unicode character sets correctly. But notice, that even the native GetFullPathName API function of Windows does not insert the required \\?\ automatically on demand. Therefore I cannot find any block of code, which can be removed from my function without removing necessary features.

Providing an additional M-file to support Unix and platform independent programs is a fair idea in the FEX. And I have never seen a software, which has been tested too exhaustively.

It is not the question, how much time is spent inside this function, but how much debug time is required to identify and fix errors caused by unusual path names, which are not treated correctly.

Counting code lines -including comment lines and test code- is a strange method to assess the code quality. If you think, you can write some code, which offers the same functionality with less lines, you are invited to do so and publish it here also. You can find more submissions here for this topic, but they consider less exceptions. Therefore I'm still convinced, that this is considered high quality code. Of course, I'd be glad, if such a function is included in Matlab already.

I appreciate any constructive feedback, but "this is more complicated than I expect" is not a helpful criticism, because this depends mainly on your expectations and not on the code.

23 Mar 2014 GetFullPath Full path name for partial or relative path Jan Simon

@Charles: I do not see a reason for down-rating, because it is not my fault that there is no built-in function yet.

16 Feb 2014 VChooseK Choose K elements from a vector - MEX: 100 times faster than NCHOOSEK Jan Simon

@Deepak Gawali: You can compile this C-code with MSVC2010 without any modifications.

16 Feb 2014 Limit figure size Set minimal or maximal size of a figure Jan Simon

Stephane: Thanks for your comment. I do not have R2013a to test or fix this.
Please feel free to send me suggestions for simplifications to the email address taken from the code.

08 Feb 2014 iseven Determine if a number is even or odd with logical true or false in the form of a 1 or 0 return value Jan Simon

9999999999999999 is not useful:
a = 9999999999999999
a == a + 1 % replies TRUE!
Better use 9007199254740992, which is 2^53, because this is the larges number representable exactly as IEEE754 double.

01 Jan 2014 Euclidean Distance Calculates euclidean distance between two vectors. Jan Simon

This slightly modified version is 30% faster for 800x800 matrices:

function z = mydist(w,p)
S = size(w, 1);
Q = size(p, 2);
z = zeros(S, Q);
for i = 1:S
ww = w(i, :) .';
for j = 1:Q
v = ww - p(:, j);
z(i, j) = v' * v;
end
end
z = sqrt(z);

14 Dec 2013 RunLength RunLength coding as fast MEX and M-code Jan Simon

Thanks for the comments, Oleg. I'm going to enhance InstallMex to let a dialog box appear to let the user decide, if this is worth a warning or an error.

The 2nd input N was thought to be created by a former call to RunLength with 1 input argument. But there is actually no good reason to restrict the types of the 2nd input. It is only inconvenient to care about all different types of inputs inside the C-Mex: The only efficient approach is to create a subfunction for each type, because an internal conversion must consider e.g. overflows of UINT64 data when converted to DOUBLE. So I decided to let the user decide if "RunLength(B, double(N))" is sufficient. But for (U)INT16/32 this problem cannot occur and three extra lines of code are sufficient. Therefore I will add this in the next version.

14 Dec 2013 Sum of vectors Gives sum of vectors. Jan Simon

This function does not work, because the input argument "v" has been forgotten. Calculating a sum by a loop is a very bad idea in Matlab, because the built-in function SUM() is much nicer, faster and free of bugs.

I do not see a reason to publish this not working piece of code without help text, which performs calculations slower than an already existing function. Please remove it from the FEX.

14 Dec 2013 Closest value Find the closest value to a given number in a vector Jan Simon

@havfal: It depends. If the vector to search in is large (e.g. millions of numbers) and sorted, the binary search in HISTC is a real benefit. But testing the sorting status in a FOR loop is a massive waste of time.

"min(vec)>num || max(vec)<num" is slow also, when it is an exception. Is the searched values are inside the interval, checking for out of bound values should be performed after HISTC only if the first or last bin is found.

14 Dec 2013 Fast or Accurate Moving Average (mex functions) Return the moving average of 2D matrix X, given a running window w Jan Simon

Calculating the sum at first an dividing by the window length afterwards is not efficient, because this doubles the memory requirements. It would be faster to perform the division directly in the C-code.

The inplace operations in C are nicer and faster:
y[ii * m2] = y[ii * m2] + x[ii * m + hh];
Better:
y[ii * m2] += x[ii * m + hh]

Using INT as type for indexing operations is a bad idea in times of 32 and 64 bit machines. Better rely on the well defined types mwIndex, mwSignedIndex and mwSize.

Usind NDIMS() is slightly nicer than "size(size(X), 2) == 2". The overhead of checking the size and type of the input is much smaller, when you implement it inside the MEX function. For small arrays almost all of the time is spent with the checks in the M-file now, while such checks have almost no measurable dely inside a MEX function.

29 Nov 2013 Closest value Find the closest value to a given number in a vector Jan Simon

[value, index] = min(abs(vec - num))

25 Oct 2013 FileRename Fast renaming of files or folders Jan Simon

@Magnus: I'm glad that this function is useful. Yes, I have a C-Mex replacement for COPYFILE also. It is about twice as fast. I did not publish it yet: Windows only, no Unicode support. But feel free to contact me by email.

25 Oct 2013 DateStr2Num Convert date string to date number - C-Mex: much faster than DATENUM Jan Simon

@James: I do not understand the question. Of course I've created the C-file, before it could be compiled by the mex command.
I suggest to post the error you get form the compiler and explain the required details.

25 Oct 2013 Vandermonde matrix, vander() function extension Adds functionality to the original MATLAB vander() function. Jan Simon

It is a very bad idea to shadow the original function "vander" by this version. The new feature, which allows the specification of the number of columns might be useful, but the order of columns has changed. In consequence several toolbox functions will reply wrong results.

14 Oct 2013 Multi Plot with marker spacing plot large 2D data set with custom marker spacing Jan Simon

A better "remove zeros" with inplace processing:

function X = rmz(X)
X = X(X~=0);