diff --git a/README b/README deleted file mode 100644 index d1d3068..0000000 --- a/README +++ /dev/null @@ -1,10 +0,0 @@ -A set of small utilities to make programming in MATLAB, including GUI development, more productive. These utilities aren't tied to a specific field of study, and are intended to be useful for general numerical work. - -The highlights are: a version of ls that shows the number of lines of code in each M file, with clickable links to open MATLAB-based files and utilities to simplify GUI programming by allowing easier access to handle properties. - -There are also simple MATLAB functions for accessing simple routines in git and svn, designed to allow some interaction with version control from within MATLAB. - -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. - -Jonathan R. Birge -birge@mit.edu diff --git a/README.md b/README.md new file mode 100644 index 0000000..f983bfb --- /dev/null +++ b/README.md @@ -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. diff --git a/examples/screenshot.png b/examples/screenshot.png new file mode 100644 index 0000000..4046d06 Binary files /dev/null and b/examples/screenshot.png differ diff --git a/examples/test_hist2d.m b/examples/test_hist2d.m new file mode 100644 index 0000000..ac05df5 --- /dev/null +++ b/examples/test_hist2d.m @@ -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) + \ No newline at end of file diff --git a/examples/test_plot.m b/examples/test_plot.m new file mode 100644 index 0000000..6ded6e3 --- /dev/null +++ b/examples/test_plot.m @@ -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 diff --git a/factorialv.m b/factorialv.m index f276fbc..ba9206f 100644 --- a/factorialv.m +++ b/factorialv.m @@ -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, @@ -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 diff --git a/getdata.m b/getdata.m deleted file mode 100644 index d28e96f..0000000 --- a/getdata.m +++ /dev/null @@ -1,6 +0,0 @@ -function data = getdata(tag, prop) -%GETDATA Retrieve property from GUI given a tag name. -% This function is mean to be called from a callback, as it uses gcbf. - -handle = findobj(gcbf, 'Tag', tag); -data = get(handle, prop); diff --git a/gethandle.m b/gethandle.m deleted file mode 100644 index 11838bb..0000000 --- a/gethandle.m +++ /dev/null @@ -1,12 +0,0 @@ -function hdl = gethandle(tag) -%GETHANDLE Retrieve handles of objects given a tag name. -% Will retrieve a list of handles given a cell array of tags. - -if iscell(tag) - hdl = zeros(1, length(tag)); - parfor i = 1:length(tag), - hdl(i) = findobj(gcbf, 'Tag', tag{i}); - end -else - hdl = findobj(gcbf, 'Tag', tag); -end diff --git a/getstr.m b/getstr.m deleted file mode 100644 index 2ea3514..0000000 --- a/getstr.m +++ /dev/null @@ -1,5 +0,0 @@ -function str = getstr(tag) -%GETSTR Retrieve string of edit box object given its tag. - -handle = findobj(gcbf, 'Tag', tag); -str = get(handle, 'String'); diff --git a/getval.m b/getval.m deleted file mode 100644 index 469c7dd..0000000 --- a/getval.m +++ /dev/null @@ -1,14 +0,0 @@ -function val = getval(tag) -%GETVAL Retrieve numeric value associated with GUI object given its tag. -% This returns the number in the 'Value' property, if present, -% or converts the 'String' property into a number, in the case of an -% Edit or Text box. In the latter case, if the string cannot be converted -% this function just passes along the NaN given by str2double. - -handle = findobj(gcbf, 'Tag', tag); -style = get(handle, 'Style'); -if (strcmp(style, 'edit') || strcmp(style, 'text')) - val = str2double(get(handle, 'String')); -else - val = get(handle, 'Value'); -end diff --git a/gitlog.m b/gitlog.m index a5375e5..f05da74 100644 --- a/gitlog.m +++ b/gitlog.m @@ -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 diff --git a/gitst.m b/gitst.m index 10f3a75..b71d569 100644 --- a/gitst.m +++ b/gitst.m @@ -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 diff --git a/hist2d.m b/hist2d.m index 8766671..2b58d16 100644 --- a/hist2d.m +++ b/hist2d.m @@ -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); @@ -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 diff --git a/ls.m b/ls.m index aa2a909..28fdf37 100644 --- a/ls.m +++ b/ls.m @@ -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)); @@ -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,'.') @@ -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; @@ -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]; @@ -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); @@ -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}]; @@ -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 @@ -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); diff --git a/makepretty.m b/makepretty.m new file mode 100644 index 0000000..dec262f --- /dev/null +++ b/makepretty.m @@ -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 diff --git a/msize.m b/msize.m index 7e21de9..e3ea190 100644 --- a/msize.m +++ b/msize.m @@ -9,7 +9,7 @@ mfiles = 0; if (level <= depthlimit) && (dirname(end) ~= '.' || level == 0) ds = dir(dirname); - for k = 1:length(ds), + for k = 1:length(ds) d = ds(k); filename = d.name; if d.isdir diff --git a/mtimes2.m b/mtimes2.m index 99645b5..f4b6297 100644 --- a/mtimes2.m +++ b/mtimes2.m @@ -7,7 +7,7 @@ N = length(varargin); A = varargin{N}; -for i = (N-1):-1:1, +for i = (N-1):-1:1 B = varargin{i}; @@ -23,16 +23,16 @@ % Case I: A or B is a single matrix if (Az == 1) - for z = 1:Bz, + for z = 1:Bz C(:,:,z) = B(:,:,z) * A; end else if (Bz == 1) - for z = 1:Az, + for z = 1:Az C(:,:,z) = B * A(:,:,z); end else % Case II: A and B have the same size. - for z = 1:Az, + for z = 1:Az C(:,:,z) = B(:,:,z) * A(:,:,z); end end diff --git a/myplot.m b/myplot.m index 4d8bf3f..98bedfb 100644 --- a/myplot.m +++ b/myplot.m @@ -1,35 +1,25 @@ function h = myplot(varargin) -%MYPLOT Custom plotting - -% grey = [0.5 0.5 0.5]; -black = [0 0 0]; -white = [1 1 1]; -% blue = [0.2 0.7 0.9]; -% darkblue = [0 .25 0.5]; - -hdls = plot(varargin{:}); +%MYPLOT 2D plot using custom settings +% myplot(...) takes the same parameters as the default MATLAB plot +% routine. In addition, the command form 'myplot x y' will plot the +% variables named x and y from the workspace, labeling the axes of the +% plot as such. + +if ischar(varargin{1}) && ischar(varargin{2}) % command form + xname = varargin{1}; + yname = varargin{2}; + xval = evalin('caller', xname); + yval = evalin('caller', yname); + hdls = plot(xval, yval, varargin{3:end}); + xlabel(xname) + ylabel(yname) +else % normal form + hdls = plot(varargin{:}); +end -axh = gca; +% make it look nice fh = gcf; - -set(hdls, 'LineWidth', 4) -% if length(hdls) == 1 -% set(hdls, 'Color', blue) -% end -% set(hdls, 'LineWidth', 3, 'Color', black) - -set(fh, 'Color', white) - -xdat = get(hdls(1), 'XData'); -xlim([min(xdat) max(xdat)]) - -set(axh, ... - 'FontName', 'Arial', 'Color', white, 'FontSize', 18, 'FontWeight', 'Bold', ... - 'GridLineStyle', '-', 'GridColor', 0.5*ones(1,3), ... - 'MinorGridLineStyle', '-', 'MinorGridColor', 0.5*ones(1,3), ... - 'XGrid', 'off', 'XColor', black, 'XMinorTick', 'off', ... - 'YGrid', 'off', 'YColor', black, 'YMinorTick', 'off', ... - 'LineWidth', 2) +makepretty(fh); if nargout > 0 h = hdls; diff --git a/nfo.m b/nfo.m index 37f3c04..10a718b 100644 --- a/nfo.m +++ b/nfo.m @@ -1,12 +1,14 @@ function nfo(v) %NFO Summary information about vector. % nfo(v) prints summary information about the vector v, including the mean, -% extremes, standard deviation, mode, etc. +% extremes, standard deviation, etc. [m, n] = size(v); + if min(m, n) > 1 error('nfo:size', 'nfo must be called with a vector, not a matrix') end + fprintf('size \t= \t%d x %d\nmin \t= \t%f\nmax \t= \t%f\nmean \t= \t%f\nstd \t= \t%f\n', ... m, n, min(v), max(v), mean(v), std(v)) diff --git a/pbino.m b/pbino.m index 8b83494..652224d 100644 --- a/pbino.m +++ b/pbino.m @@ -5,6 +5,6 @@ % version, however, nor is it valid for very large n. p = zeros(size(r)); -parfor i = 1:length(r), +parfor i = 1:length(r) p(i) = nchoosek(n, r(i))*pi^r(i)*(1 - pi)^(n - r(i)); end diff --git a/polyderiv.m b/polyderiv.m index 6318974..aec54e9 100644 --- a/polyderiv.m +++ b/polyderiv.m @@ -1,4 +1,4 @@ function dc = polyderiv(c) n = length(c) - 1; -dc = c(1:n) .* [n:-1:1]; \ No newline at end of file +dc = c(1:n) .* (n:-1:1); \ No newline at end of file diff --git a/polynewton.m b/polynewton.m index ee8213c..8ba7ee7 100644 --- a/polynewton.m +++ b/polynewton.m @@ -1,8 +1,10 @@ function r = polynewton(p, r, n) -if (nargin < 3) n = 1; end +if (nargin < 3) + n = 1; +end dp = polyderiv(p); -for k = 1:n, +for k = 1:n H1 = polyval(p, r); H2 = polyval(dp, r); r = r - H1./H2; diff --git a/setdata.m b/setdata.m deleted file mode 100644 index ea121e3..0000000 --- a/setdata.m +++ /dev/null @@ -1,12 +0,0 @@ -function setdata(tag, prop, data) -%SETDATA Set property of a handle object given its tag. - -if iscell(tag) - for i = 1:length(tag), - handle = findobj(gcbf, 'Tag', tag{i}); - set(handle, prop, data); - end -else - handle = findobj(gcbf, 'Tag', tag); - set(handle, prop, data); -end \ No newline at end of file diff --git a/setval.m b/setval.m deleted file mode 100644 index 9a51f2b..0000000 --- a/setval.m +++ /dev/null @@ -1,10 +0,0 @@ -function setval(tag, val) -%SETVAL Set numeric value of edit box given its tag. - -handle = findobj(gcbf, 'Tag', tag); -style = get(handle, 'Style'); -if (strcmp(style, 'edit') | strcmp(style, 'text')) - set(handle, 'String', num2str(val)); -else - set(handle, 'Value', val); -end diff --git a/sn.m b/sn.m index 9a16143..23f4807 100644 --- a/sn.m +++ b/sn.m @@ -73,7 +73,7 @@ function dirscan(dirname, suffix, depthlimit, level) if length(ds) == 1 && strcmp(ds.name, dirname) dirname = '.'; end - for k = 1:length(ds), + for k = 1:length(ds) d = ds(k); if d.isdir dirscan([dirname '/' d.name], suffix, depthlimit, level + 1); diff --git a/svndumpoblit.m b/svndumpoblit.m deleted file mode 100644 index 7377d22..0000000 --- a/svndumpoblit.m +++ /dev/null @@ -1,94 +0,0 @@ -function svndumpoblit(exsuff, filein, fileout) - -% Input handling. -if nargin < 1 || isempty(exsuff) - exsuff = {'\.mexw32', '\.mexglx', '\.mexmac', '\.mexmaci64', '\.mexa64', 'mex\.dll'}; -end - -if nargin < 2 - [fnamein, pathin] = uigetfile('*', 'Pick a dump file'); - [fnameout, pathout] = uiputfile('*.dump', 'Select output file'); -else - pathin = ''; - pathout = ''; - fnamein = filein; - fnameout = fileout; -end - -% Build regular expression. -if iscell(exsuff) - testregexp = exsuff{1}; - for k = 2:length(exsuff) - testregexp = [testregexp '|' exsuff{k}]; - end -else - testregexp = exsuff; -end - -% Setup tokens. -revtoken = 'Revision-number:'; -nodetoken = 'Node-path:'; -contoken = 'Content-length'; -revtoklen = length(revtoken); -nodetoklen = length(nodetoken); -contoklen = length(contoken); - -% Open files. -fin = fopen([pathin fnamein]); -fout = fopen([pathout fnameout], 'w'); - -% Cycle through file. -saved = 0; -nodes = 0; -copyflag = true; %#ok<*NASGU> % copy while true -changeflag = false; -contlen = 0; -if fin ~= -1 - dline = fgets(fin); - while ischar(dline) - % Limited parsing. - if strncmp(dline, revtoken, revtoklen) - rev = str2double(dline(revtoklen+2:end-1)); - fprintf('Revision: %d\n', rev); - copyflag = true; - elseif strncmp(dline, nodetoken, nodetoklen) % test for node - if isempty(regexp(dline, testregexp, 'once')) % test for file - copyflag = true; - else - fprintf('%s...\n', dline(nodetoklen+2:end-1)); - copyflag = false; - end - elseif strncmp(dline, contoken, contoklen) - contlen = str2double(dline(contoklen+2:end-1)); % bytes - end - - % Copy parsed line through? - if copyflag - fwrite(fout, dline, 'char'); - end - - % Fast forward? - if contlen > 0 - if copyflag - block = fread(fin, contlen, 'char'); - fwrite(fout, block, 'char'); - else - saved = saved + ceil(contlen/1000); - nodes = nodes + 1; - fseek(fin, contlen, 'cof'); - end - contlen = 0; - end - - % Read in next line. - dline = fgets(fin); - end % read while loop - - fclose(fin); - fclose(fout); - - fprintf('Saved %d kB over %d nodes\n', saved, nodes) -end % if file opened - -end % main function - diff --git a/svnst.m b/svnst.m deleted file mode 100644 index 5ec5464..0000000 --- a/svnst.m +++ /dev/null @@ -1,23 +0,0 @@ -function svnst -%SVNSTAT Trivial function to get simple svn info -% Obtains the status and information about the current directory from the -% system svn command. - -[status, result] = systemwpath('svn info'); -if status == 0 - fprintf('svn info:\n') - fprintf(result) -else - error('svnst:svn', 'svn command not found in system') -end - -[status, result] = systemwpath('svn status'); -if status == 0 - bls = regexp(result, '\n\n'); - result(bls) = []; % remove annoying blank lines that svn always puts in - fprintf('svn status:\n') - fprintf(result) -end - -end - diff --git a/w.m b/w.m new file mode 100644 index 0000000..c0d2671 --- /dev/null +++ b/w.m @@ -0,0 +1,30 @@ +function vartabout = w() +%W Print workspace information as a table +% The main difference between this and the standard whos command is that +% this returns the variables as a MATLAB table object. + +vars = evalin('caller', 'whos()'); +n = length(vars); +NameC = cell(n, 1); +SizeC = cell(n, 1); +MemC = cell(n, 1); +ClassC = cell(n, 1); +for k = 1:n + v = vars(k); + NameC{k} = v.name; + SizeC{k} = [num2str(v.size(1)) ' x ' num2str(v.size(2))]; + MemC{k} = makesizestr(ceil(v.bytes/1024)); + ClassC{k} = v.class; +end +Size = categorical(SizeC); +Mem = categorical(MemC); +Class = categorical(ClassC); + +vartab = table(Size, Mem, Class, 'RowNames', NameC); + +if nargout == 0 + disp(vartab) + fprintf('\n') +else + vartabout = vartab; +end