Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bunch of cleanup and fixes #7

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions README

This file was deleted.

25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# MATLAB Utilities

## Overview

A set of small command utilities to make programming in MATLAB more productive. These utilities aren't tied to a specific field of study, and are intended to be useful for general work.

## Highlights

* A version of ls that shows the number of lines of code in each M file, with clickable links to open MATLAB-based files
* Simple MATLAB wrappers for git and svn, designed to allow some integration of version control from within the MATLAB command window.
* Plotting routines which automatically use better formatting than the default MATLAB plotting, and can use symantic knowledge of the passed variables to add axis labels (e.g. 'plot x y' will plot the variables x versus y and label the axes as such).

## Example

`>> Time = linspace(0, 1.12, 256);`

`>> Signal = sin(4*pi*Time.^2)*0.94;`

`>> myplot Time Signal`

![screenshot](./examples/screenshot.png)

## Notes

The addition of any similarly generally applicable tools developed by others would be a welcome addition, as I'd like to see this become a large set of general tools to be shared.
Binary file added examples/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions examples/test_hist2d.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
x = randn(1, 10000);
y = x + randn(1, 10000);

figure(1)
histogram2(x, y, 31)

figure(2)
hist2d(x, y, 31)

10 changes: 10 additions & 0 deletions examples/test_plot.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
% test plotting routines on a 2D plot with weird limits

Time = linspace(0, 1.12, 256);
Signal = sin(4*pi*Time.^2)*0.94;

figure(1)
plot(Time, Signal)

figure(2)
myplot Time Signal
4 changes: 2 additions & 2 deletions factorialv.m
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function p = factorialv(n)
%FACTORIAL Vectorized factorial function.
%FACTORIALV Vectorized factorial function.
% FACTORIAL(N) is the product of all the integers from 1 to N,
% i.e. prod(1:N). Since double precision numbers only have about
% 15 digits, the answer is only accurate for N <= 21. For larger N,
Expand All @@ -9,6 +9,6 @@
% See also PROD.

N = length(n);
parfor k = 1:N,
parfor k = 1:N
p(k) = prod(1:n(k));
end
6 changes: 0 additions & 6 deletions getdata.m

This file was deleted.

12 changes: 0 additions & 12 deletions gethandle.m

This file was deleted.

5 changes: 0 additions & 5 deletions getstr.m

This file was deleted.

14 changes: 0 additions & 14 deletions getval.m

This file was deleted.

6 changes: 3 additions & 3 deletions gitlog.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
%GITST Trivial function to get git status
% Obtains the status and information about the current directory from the
% system git command. Useful only in that it lets you check git status
% without having to go to a terminal.
% without having to go to a terminal. Only works on Linux or Mac OS X.

[status, result] = systemwpath('git --no-pager log --graph --oneline --decorate --all | head -n 16');

if status == 0
fprintf('git log:\n')
fprintf(result)
disp(result)
else
error('gitlog:git', result)
end
Expand Down
7 changes: 2 additions & 5 deletions gitst.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@
% system git command. Useful only in that it lets you check git status
% without having to go to a terminal.

[status, result] = systemwpath('git status');
[status, result] = systemwpath('git status -s -b');
if status == 0
fprintf('git status:\n')
fprintf(result)
disp(result)
else
error('gitst:git', result)
end

end
66 changes: 41 additions & 25 deletions hist2d.m
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
function [phi, xc, yc] = hist2d(x, y, n)
%HIST2 Two-dimensional histogram.
function [phi, xc, yc] = hist2d(x, y, n, type)
%HIST2 Simple two-dimensional histogram
% hist2d(x, y, n) plots the n x n histogram of the data contained in the x
% and y vectors. This function has been rendered obsolute by the MATLAB
% histogram2 function introduced in 2015.

if (nargin < 3)
n = 10; % default bins
if nargin < 4
type = 'bar';
if nargin < 3
n = 10; % default bins per dimension
end
end

xmin = min(x);
Expand All @@ -15,34 +21,44 @@
yedges = linspace(ymin, ymax, n + 1);

% Generate histograms along y-axis.
for i = 1:n,
yi = y(x >= xedges(i) & x < xedges(i + 1));
if ~isempty(yi)
yhisti = histc(yi, yedges);
else
yhisti = zeros(1, n+1);
end
h(:,i) = fliplr(yhisti(1:(end - 1)))';
h = zeros(n);
for i = 1:n
yi = y(x >= xedges(i) & x < xedges(i + 1));
if ~isempty(yi)
yhisti = histcounts(yi, yedges);
else
yhisti = zeros(1, n);
end
h(:,i) = fliplr(yhisti).';
end

xc = (xedges(1:end-1) + xedges(2:end))/2;
yc = (yedges(1:end-1) + yedges(2:end))/2;

% Plot or output mass matrix.
% Note: the matrix h is arranged as y->i, x->j
if (nargout == 0)
% Note: the matrix h is arranged as y->i, x->j
imagesc(h)
colorbar

xcenters = (xedges(2:end) + xedges(1:end-1))/2;
ycenters = (yedges(2:end) + yedges(1:end-1))/2;
xlabels = xcenters(get(gca, 'XTick'));
ylabels = ycenters(get(gca, 'YTick'));
set(gca, 'XTickLabel', num2str(xlabels'));
set(gca, 'YTickLabel', num2str(fliplr(ylabels)'));
xlabel('\itx')
ylabel('\ity')
%bar3(xc, h, 1, 'hist')
if strcmp(type, 'image')
imagesc(h)
colorbar
xcenters = (xedges(2:end) + xedges(1:end-1))/2;
ycenters = (yedges(2:end) + yedges(1:end-1))/2;
xlabels = xcenters(get(gca, 'XTick'));
ylabels = ycenters(get(gca, 'YTick'));
set(gca, 'XTickLabel', num2str(xlabels'));
set(gca, 'YTickLabel', num2str(fliplr(ylabels)'));
xlabel('\itX')
ylabel('\itY')
else
bar3(xc, h, 1, 'hist')
xtickn = length(get(gca, 'XTick'));
ytickn = length(get(gca, 'YTick'));
xcenters = linspace(xedges(1), xedges(end), xtickn);
ycenters = linspace(yedges(1), yedges(end), ytickn);
set(gca, 'XTickLabel', num2str(xcenters', 2));
set(gca, 'YTickLabel', num2str(fliplr(ycenters)', 2));
axis('tight')
end
else
phi = h;
end
32 changes: 16 additions & 16 deletions ls.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@

%%% Display directory first.
cdir = pwd;
if length(cdir) > colwidth;
if length(cdir) > colwidth
slashes = find((cdir == '\') | (cdir == '/'));
wd = cdir(slashes(end-1):end);
head = cdir(1:slashes(3));
Expand All @@ -68,7 +68,7 @@

%%% Cull files.
hitlist = true(1,n);
for krow = 1:n,
for krow = 1:n
d = ds(krow);
namestr = d.name;
if strcmp(namestr,'.')
Expand All @@ -91,7 +91,7 @@
isdir = false(n,1);
dolink = false(n,1);
typeindex = zeros(n,1); % sorting priority
for k = 1:n,
for k = 1:n
d = ds(k);
filestr = d.name;

Expand All @@ -108,31 +108,31 @@
dolink(k) = true;
typelen = length(typestr);
switch typestr
case 'm',
case 'm'
f = fopen(filestr);
mline = fgets(f);
firstline = mline;
fclose(f);
if strfind(firstline, 'classdef')
if contains(firstline, 'classdef')
typeindex(k) = 99;
dirsym = '@';
elseif strfind(firstline, 'function')
elseif contains(firstline, 'function')
typeindex(k) = 95;
dirsym = '*';
else
typeindex(k) = 90;
dirsym = '$';
end
case 'mat',
case 'mat'
typeindex(k) = 80;
dirsym = '#';
case 'fig',
case 'fig'
typeindex(k) = 70;
dirsym = '+';
case 'mdl',
case 'mdl'
typeindex(k) = 60;
dirsym = '&';
otherwise,
otherwise
dolink(k) = false;
typeindex(k) = -int8(typestr(1));
dirsym = ['.' typestr];
Expand Down Expand Up @@ -176,8 +176,8 @@
%%% Output.
if printq % write to terminal
nrows = ceil(n/ncols);
for krow = 1:nrows,
for kcol = 1:ncols,
for krow = 1:nrows
for kcol = 1:ncols
ksort = krow + (kcol-1)*nrows; % which sorted element we're on
if ksort <= n
k = p(ksort);
Expand All @@ -202,7 +202,7 @@
closetag = '';
end
varlen = countlen + length(sizestrs{k}) + length(typestrs{k});
if (namelen + varlen) > (maxlen - 1), % filename too big
if (namelen + varlen) > (maxlen - 1) % filename too big
nameline = [countstr opentag ...
namestrs{k}(1:max(maxlen-varlen-contlen-1,1)) ...
closetag contstr typestrs{k} ' ' sizestrs{k}];
Expand All @@ -224,11 +224,11 @@
if printq
if summary
[mlinecount, mfilecount] = msize('.', 2);
sumstr = sprintf('mfiles: %d lines in %d files', ...
sumstr = sprintf('matlab: %d lines in %d files', ...
mlinecount, mfilecount);
fprintf([repmat(' ', 1, colwidth + 2 - length(sumstr)) '[' sumstr ']\n'])

sumstr = sprintf([makesizestr(sizesum) ' in %d files, %d dirs'], ...
sumstr = sprintf(['all: ' makesizestr(sizesum) ' in %d files, %d dirs'], ...
filecount, dircount);
fprintf([repmat(' ', 1, colwidth + 2 - length(sumstr)) '[' sumstr ']\n'])
else
Expand Down Expand Up @@ -257,7 +257,7 @@
s = 0;
if (level <= depthlimit) && (dirname(end) ~= '.')
ds = dir(dirname);
for k = 1:length(ds),
for k = 1:length(ds)
d = ds(k);
if d.isdir
sdir = dirsize([dirname '\' d.name], depthlimit, level + 1);
Expand Down
51 changes: 51 additions & 0 deletions makepretty.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
function makepretty(fig)

% input handling
if nargin > 1
setfig = true;
oldfh = gcf;
figure(fig)
else
setfig = false;
end

% color definitions;
foregnd = [0 0 0];
backgnd = [1 1 1];

% figure settings
axh = gca;
fh = gcf;
set(fh, 'Color', backgnd)

% dynamically set linewidths
hdls = get(axh, 'Children');
for kline = 1:length(hdls)
ndat = length(hdls(kline).XData);
tline = min(ceil(3*512/ndat), 4);
hdls(kline).LineWidth = tline;
end

% axis limits
xdat = get(hdls, 'XData');
if iscell(xdat)
xlim([min(xdat{1}) max(xdat{1})])
else
xlim([min(xdat) max(xdat)])
end

% axis line and text styling
set(axh, ...
'FontName', 'Arial', 'FontSize', 14, 'FontWeight', 'Bold', ...
'GridLineStyle', '-', 'MinorGridLineStyle', '-', 'Color', backgnd, 'LineWidth', 1, ...
'XGrid', 'off', 'XColor', foregnd, 'XMinorTick', 'on', ...
'YGrid', 'off', 'YColor', foregnd, 'YMinorTick', 'on')

% axis label styling
labs = findall(gcf, 'Type', 'text');
set(labs, 'FontName', 'Arial', 'FontSize', 16, 'FontWeight', 'Bold');

% reset current figure
if setfig
figure(oldfh);
end
Loading