Entries Tagged as 'Technical'

niceplot: Preparing Camera-Ready Plots in Matlab

[This post is recovered from my old site kaisare.net and posted here with minor modifications. Originally posted in October 2005.] 

Here is a Matlab code for making camera-ready plots for presentations or papers. I found these values to be the optimum for journals that use two-column format. Save the figure as eps for use with LaTeX, or copy it and paste in a word file. If you do the latter, make sure that in Edit >> Copy Options >> Clipboard Format, the option “Preserve information (metafile if possible)” is selected.

This code is released under CC Attribution 2.5 license. You may use, modify, share or sell this code provided the second line (”Niket Kaisare, nkaisare [at] gmail.com”) is kept intact. I assume no liability for any problems the use of this code may cause.


% Niceplot: Make print-ready figures
% Niket Kaisare, nkaisare [at] gmail.com
%
% Usage:
%    niceplot(x1, y1[, s1], [x2, y2[, s2], ...], ...
%       xlabel, ylabel, title)
% Where:
%    (x1, y1), (x2, y2), ..., are data to be plotted
% Optional: String (s1, s2, ...) to specify line styles
% Required: X axis label, Y axis label and Title
%           Use blank [] if none exist
%
% Examples
%    niceplot(a, b, '-bx', 'hour', 'rainfall (in)', [])
%    niceplot(a, b, x, y, [], [], 'Brownian Motion')

function niceplot(varargin)n = nargin;
xl = varargin{n-2};     % X Label
yl = varargin{n-1};     % Y Label
tt = varargin{n};       % Axis Title
plot(varargin{1:n-3});  % Plotting Data

% Make lines thicker
lin = get(gca,’children’);
for i = 1:length(lin)
    set(lin(i), ‘linewidth’, 2.0);
end

% Make nice title
if (tt)
    set(gca,’position’, [0.13, 0.13, 0.83, 0.78]);
    title(tt,’fontname’,’times’,’fontsize’, 20);
else
    set(gca,’position’, [0.13, 0.13, 0.83, 0.8]);
end
% Make nice axes labels
set(gca,’fontname’,’times’,’fontsize’, 20);
xlabel(xl,’fontname’,’times’,’fontsize’, 20);
ylabel(yl,’fontname’,’times’,’fontsize’, 20);

Matlab(TM): Reading/loading multiple files

I got this piece of Matlab(TM) code by searching through google cache. 

This is a question I get asked frequently; most recently by a friend who was visiting this week. He has several data files saved in a single directory, which he needs to read and process recursively. All the files are named myfile###.mat. Here is a run-down on how to do this in Matlab.

The main command you need to know for this purpose is eval. Eval parses the string constant and evaluates its result, as if this string was entered on Matlab command line. For example, the following two statements are equivalent:
>> a1 = 5;
>> eval(['a1 = 5;']);

Note that the string passed on to eval is exactly the same as that executed on the command line.

Lets play a little more with the string constant being passed on to it.
>> i = 1
>> ['a', num2str(i), ' = 5;']

This command returns the string: ‘a1 = 5;’
Hint: be careful about the spaces. This command converts the value of integer i into a string constant ('1') and concatenates it with the other strings.

Consequently, the following gives the same result as the first command:
>> eval([’a’, num2str(i), ‘ = 5;’])

Sequential File Reading

Equipped with this knowledge, we will use the eval command for sequentilly reading files
for i = 1:100
    eval(['load myfile', num2str(i)])
    % Other statements for processing data
end

An improved method for more complicated names

I use the above method for reading and parsing files when the counter i has a certain meaning, such as, say length of a reactor that I am studying. However, there is another method that I use when the names are not exactly as straightforward.

For example, I need to recursively read files “myfile###.mat”, where ### can be any alphanumeric string. Lets say that the alphanumeric string ### has a certain meaning. Among other things, each files contains temperatures, and I want to read the maximum value of the temperatures for all these files. For this purpose, I will use the dir command (which gives directory listing in Matlab). The code is as follows:

dirList = dir;
parsedTData = [];
for i = 3:length(dir)
    currFile = dirList(i).name;
    % Thanks Rick! (See comment 1)

    if( length(currFile) < 6 )
        cycle% Skip short file names
    end
    if ( currFile(1:6) ~= ‘myfile’)
        cycle % Skip files not starting with ‘myfile’
    end
    mnemonic = currFile(7:end-4); % Obtain part of the
    file name between ‘myfile’ and ‘.mat’

    load(currFile);
    maxT = max(temperature);
    temp{1} = mnemonic; temp{2} = maxT;
    parsedTData = [parsedTData; temp];
end

Thats All Folks! Hope this helps.

Update: Modified the code according to comment from Rick.

Making inset figures in Matlab

Originally posted in June 2004, this was one of the most read posts; the search term “inset” generated the largest number of hits to my site, more than either “Niket” or “Kaisare”. Here is the post, restored thanks to Google cache.

Some time back, I wanted to create inset figures in Matlab(TM). I tried asking friends and help desk, but got no help. So, I tried experimenting and finally found a way to do it. Its really neat!

% Generating inset plots in Matlab
h1 = figure(1);
% h1 now has "handle" to the figure
plot(cumsum(randn(100,1)))
h2 = get(h1,'CurrentAxes');
% h2 now has "handle" to the cur
h3 = axes('pos',[.5 .2 .35 .35]);
% We specify an INSET axis.
% This axis has its origin at RELATIVE location (0.5, 0.2)
% The X- and Y-axes lengths are both 0.35 (i.e. 35% of main figure)
% h3 has the handle to this INSET figure

plot([0:0.1:10],sin([0:0.1:10]))
% Plot on the inset

set(h1,'CurrentAxes',h2)
hold on; plot(cumsum(randn(100,1)),'r')
% In order to plot on the main figure, we need to select it
% This is done using the axis handle h2, which is passed as
% the Current Axis for the figure handle h1
% Next, we plot another plot on main area

set(h1,'CurrentAxes',h3)
hold on; plot([0:0.1:10],cos([0:0.1:10]),'r')
% To plot another figure in inset, we need to select it first.
% Again follow the same procedure as above

plot([0:10],[0:0.1:1],'k')
% Where will this get plotted?
% Remember that currently selected axis is the inset axis
% Hence it will get plotted on the inset plot