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

The best of all worlds - disp + sprintf

| categories: matlab, octave | View Comments

Going through loops you would often find yourself writing something ugly like :

1: n=10;
2: is_verbose=true;
3: for i=1:n
4:  if(is_verbose)
5:   disp(sprintf('DBG: %d/%d',i,n));
6:  end
7:   % some interesting stuff here....
8: end
>> >> DBG: 1/10
DBG: 2/10
DBG: 3/10
DBG: 4/10
DBG: 5/10
DBG: 6/10
DBG: 7/10
DBG: 8/10
DBG: 9/10
DBG: 10/10
>>

While, if you had disp and sprintf combined, you could have written a beautiful code like this :

1: is_verbose=true;
2: n=10;
3: for i=1:n
4:  verbose_disp(is_verbose,'DBG: %d/%d',i,n);
5:  % some interesting stuff here....
6: end

Thanks to Matlab's varargin this little gem could be very close to sprintf in syntax.

 1: % purpose display only if the script is in verbose mode + include sprintf 
 2: % capabilities in disp.
 3: % syntax : verbose_disp(flag_verb,form,[variable_list])
 4: % flag_verb=1 if you want to display, and 0 if you don't want to
 5: % display
 6: % form = string including formatting directions for sprintf 
 7: % variable_list = more parameters which include variables fitting
 8: % into the format "form".
 9: %
10: % see also : disp, sprintf
11: 
12: % Copyright 2013 Avi Gozolchiani (http://tiny.cc/avigoz)
13: % This program is free software: you can redistribute it and/or modify
14: % it under the terms of the GNU General Public License as published by
15: % the Free Software Foundation, either version 3 of the License, or
16: % (at your option) any later version.
17: %
18: % This program is distributed in the hope that it will be useful,
19: % but WITHOUT ANY WARRANTY; without even the implied warranty of
20: % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21: % GNU General Public License for more details.
22: %
23: % You should have received a copy of the GNU General Public License
24: % along with this program.  If not, see <http://www.gnu.org/licenses/>.
25: 
26: % $Log$
27: function verbose_disp(flag_verb,form,varargin)
28: % little input checking
29: if(nargin<2)
30:     error('verbose_disp : wrong number of arguments');
31: end                                     % if(nargin<2)
32: if(~ischar(form))
33:     error('second argument should be a character string');
34: end                                 % if(~ischar(form))
35: % if mode=verbose display the formatted string
36: if(flag_verb)
37:   s=sprintf(form,varargin{:});
38:   disp(s);
39: end % if(flag_verb)

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

org-mode source

Read and Post Comments

GMT's mysterious page dimension

| categories: matlab, octave | View Comments

GMT has been around for some time now. Their "world domination map" is a nice demonstration of its scripting capabilities and quality rendering. They only suffer from one disease. Well actually two.

  1. GMT doesn't allow gradual step-by-step fine tuning. While in matlab/octave you would use "plot(x,y)" just to see what happens, and afterwards play with the axis limits until you are satisfied, and so on - in GMT you have to worry about axis and the physical figure width and height from the very beginning. Quite a barrier I would say. That's where octgmt might come handy. It's an interface between octave and GMT that will create for you an initial script.
  2. When you try to use the package, you are likely to just produce blank pages. That's because of the heavy dependence on some default printing page size, which is hidden from the user. Your plot should just fit in this mysterious default. This was actually discussed in their mailgroup . Anyhow, you probably want to try the flag "–PAPER_MEDIA=Custom_550x580" or "–PS_MEDIA=Custom_550x580" (depending on the GMT version).

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

org-mode source

Read and Post Comments

Next Page »