Skip to content

Instantly share code, notes, and snippets.

@kyamagu
kyamagu / hash.m
Created October 29, 2013 02:57
Calculate a hash value for a string or a numeric/logical array.
function number = hash(value)
%HASH Calculate a hash for a string or a numeric/logical array.
if ~ischar(value)
value = num2str(value);
end
number = typecast(int32(java.lang.String(value).hashCode()), 'uint32');
end
@kyamagu
kyamagu / md5.m
Created October 29, 2013 02:58
Calculate MD5 digest.
function digest = md5(value)
%MD5 Calculate MD5 digest.
digest = org.apache.commons.codec.digest.DigestUtils.md5(value)';
digest = typecast(digest, 'uint8');
end
@kyamagu
kyamagu / strjoin.m
Created October 29, 2013 03:13
Concatenate an array into a single string.
function output = strjoin(input, separator)
%STRJOIN Concatenate an array into a single string.
%
% S = strjoin(C)
% S = strjoin(C, separator)
%
% Description
%
% S = strjoin(C) takes an array C and returns a string S which concatenates
% array elements with comma. C can be a cell array of strings, a character
@kyamagu
kyamagu / logger.m
Created October 29, 2013 03:20
Display a log message.
function logger( varargin )
%LOGGER Display a log message.
%
% logger(fmt, param1, param2, ...)
% logger(true)
% logger(false)
%
% LOGGER displays a log message using the printf arguments. LOGGER takes
% format string FMT followed by parameters for substituion.
%
@kyamagu
kyamagu / getOptions.m
Last active December 27, 2015 22:58
Matlab variable argument parser.
function [options, remaining_arguments] = getOptions(options, varargin)
%GETOPTIONS Get options from variable length arguments.
%
% options = getOptions(options, 'option1', value1, 'option2', value2, ...);
% [options, remaining_arguments] = getOptions(options, ...);
%
% GETOPTIONS parses variable length arguments. OPTIONS is a scalar struct of
% default values. Its fields are updated with name-value pairs of variable
% length input arguments.
%
@kyamagu
kyamagu / getOutputArguments.m
Last active December 28, 2015 03:58
Get a specified output arguments by index.
function varargout = getOutputArguments(index, function_handle, varargin)
%GETOUTPUTARGUMENTS Get specified output arguments by index.
%
% [argument1, argument2, ...] = getOutputArguments(index, function_handle)
%
% Example
% -------
%
% Getting the second output argument of min().
%
@kyamagu
kyamagu / mexopts.sh
Last active January 2, 2016 21:29
Matlab MEX configurations to be compatible with OS X Mavericks. 1) Change target to 10.9, 2) Change from libstdc++ to libc++, 3) Use c++11, 4) Add CHAR16_T macro
CC='clang'
CXX='clang++'
SDKROOT='/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/'
MACOSX_DEPLOYMENT_TARGET='10.9'
CFLAGS="$CFLAGS -Dchar16_t=uint16_t"
CXXFLAGS="$CXXFLAGS -std=c++11 -stdlib=libc++ -DCHAR16_T"
CXXLIBS="$MLIBS -lc++"
@kyamagu
kyamagu / mex-dispatch.h
Created March 13, 2014 21:59
MEX-disptach: Macro library to build a dispatchable MEX file in ANSI C.
/** MEX-disptach: Macro library to build a dispatchable MEX file.
*
* Example:
*
* // mylibrary.c
* #include "mex-dispatch.h"
* void myFunction(int nlhs, mxArray** plhs,
* int nrhs, const mxArray** prhs) {
* mexPrintf("myFunction called.");
* }
@kyamagu
kyamagu / find-opencv-conflict.sh
Last active August 29, 2015 14:02
UNIX command to detect conflicting opencv objects.
#!/bin/sh
pkg-config --libs opencv | grep -E -o "/.*\.(so|dylib)" | sed "s/ /:/g"
@kyamagu
kyamagu / java_home.sh
Created August 12, 2014 15:06
Command to find JAVA_HOME in OS X. Needed to use older Matlab that is not compatible with Java 1.7.
#!/bin/sh
/System/Library/Frameworks/JavaVM.framework/Versions/A/Commands/java_home -v "1.6"