A general slicing syntax

| categories: matlab, octave | View Comments

To slice the 4th cross section of the second dimension of a 3-dimensional array in Matlab/Octave, you would use a code like :

1: z=z(:,4,:);

If you want a bit more flexibility than that, you can have the sliced dimension as a parameter, using subsref :

1: dim=2;
2: idx.type='()';               
3: idx.subs={':',':',':'};
4: idx.subs{dim}=4;
5: z=subsref(z,idx);

Copyright (C) 2015 by Avi Gozolchiani. See the License for information about copying.

org-mode source

Read and Post Comments

save a plot in png, eps, and fig formats

| categories: matlab, octave, workflow | View Comments

When you save a figure, what you really want to save is - several formats, a fig file, all relevant data that is needed to reconstruct the fig, and a README that tells you what is there. That's the purpose of my savefigs :

 1: % purpose : save a figure in png,eps, and fig formats
 2: % syntax : savefigs(filename,readme_text,data_str)
 3: % filename - file name without any suffix (savefigs does not check this, so
 4: % if you mistakenly set filename="stam.fig", the output files will 
 5: % be stam.fig.fig, stam.fig.eps, stam.fig.png)
 6: % readme_text - a string that describes the figure, and the data.
 7: % data_str - a data structure that contains all needed info in
 8: % order to reconstruct the figure
 9: % 
10: % see also: print, hgsave
11: 
12: 
13: % Copyright 2013 Avi Gozolchiani (http://tiny.cc/avigoz)
14: % This program is free software: you can redistribute it and/or modify
15: % it under the terms of the GNU General Public License as published by
16: % the Free Software Foundation, either version 3 of the License, or
17: % (at your option) any later version.
18: %
19: % This program is distributed in the hope that it will be useful,
20: % but WITHOUT ANY WARRANTY; without even the implied warranty of
21: % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22: % GNU General Public License for more details.
23: %
24: % You should have received a copy of the GNU General Public License
25: % along with this program.  If not, see <http://www.gnu.org/licenses/>.
26: 
27: % $Log$
28: function savefigs(filename,readme_text,data_str)
29: isoctave=(exist('OCTAVE_VERSION','builtin')~=0);
30: if(isoctave)
31:     prints=struct('suff',{'png','eps'},...
32:                   'func',{@(x)print('-dpng',x),@(x)print('-depsc2',x)});    
33: else
34:     prints=struct('suff',{'png','eps','fig'},...
35:                   'func',{@(x)print('-dpng',x),@(x)print('-depsc2',x), ...
36:                         @hgsave});
37: end
38: n_printfuncs=length(prints);
39: for i_printfunc=1:n_printfuncs % fig,png, and eps files
40:     prints(i_printfunc).func([filename,'.',prints(i_printfunc).suff]);
41: end % for i_printfunc=1:n_printfuncs
42: % document what is it
43: fid=fopen([filename,'_README','.txt'],'wt');
44: fprintf(fid,'%s',readme_text);
45: fclose(fid);
46: % save the vector/matrix for future crunching
47: save([filename,'_data','.mat'],'data_str');

Copyright (C) 2015 by Avi Gozolchiani. See the License for information about copying.

org-mode source

Read and Post Comments

Regridding unequally spaced sampled field, and plotting an imagesc

| categories: matlab, octave | View Comments

In a previous post we discussed a better way for using imagesc, with a more sane colormap. Let's now speak about the grid. Imagesc will happily embed every xy-axis you plug in, without checking whether dimensions fit. If your grid is non-equally spaced - it will just draw the z-axis on a regular axis (i.e. ignoring the xy input), and show the xy-axes as if they genuinely represent the input. My conclusion from this wild behavior is - never use imagesc on non-equally spaced data. Matlab has nice interpolant interfaces to help you get your data equally gridded.

1: [x1,y1]=ndgrid(x,y); % this step is actually not crucial
2: I = griddedInterpolant(x1,y1,z);  
3: x1 = linspace(min(x),max(x),5);     % Define an equally spaced grid
4: y1 = linspace(min(y),max(y),5);
5: [x1,y1]=ndgrid(x1,y1);
6: z1=I(x1,y1);
7: myimagesc(x1(1,:),y1(:,1),z1,0.55,0.95,0.05);

Where your input parameters to myimagesc may vary, and you could replace "5" by whatever division of the equally spaced grids you fancy.

Copyright (C) 2015 by Avi Gozolchiani. See the License for information about copying.

org-mode source

Read and Post Comments

Screen - unique logs for each run

| categories: workflow, linux | View Comments

Screen is a little wrap around linux shell that enables detaching and logging out while the session you created is still running. It could be used for lots of different purposes, and running an intensive computation on a remote computer is an obvious example.

You would normally do :

1: screen -md -L -S session_name your_program

-md = detach immediately after running, and return to the current terminal session -L = create a log file -S = create meaningful name for your session

To check the stat of your sessions you will use:

1: screen -ls

To have different log files with unique names for different sessions, you need to create a ~/.screenrc file, with the following single line

logfile screenlog-%Y%m%d-%c:%s

Copyright (C) 2015 by Avi Gozolchiani. See the License for information about copying.

org-mode source

Read and Post Comments

Consistent Latex units in non italics

| categories: latex | View Comments

Yet another latex tidbit. Its purpose - remove italics from the units inside math mode.

in the header :

1: \newcommand{\unit}[1]{\ensuremath{\, \mathrm{#1}}}

in the body :

1: $\tau=0.0257\left[\unit{N\cdot m^{-2}}\right]$

If you want to take this approach to the very extreme, you could have the units of every var defined in the header :

1: \newcommand{\tauunit}{\unit{N}\cdot\unit{m}^{-2}}

And use them consistently without silly unit mistakes inside your manuscript :

1: $\tau=0.4\tauunit$

Copyright (C) 2015 by Avi Gozolchiani. See the License for information about copying.

org-mode source

Read and Post Comments

« Previous Page -- Next Page »