Showing posts with label windows. Show all posts
Showing posts with label windows. Show all posts

Saturday, May 24, 2008

External commands in MATLAB

For some time now, I have often wondered why MATLAB highlighted text starting with a ! in a different colour. I thought nothing of it until recently when I was, as always, randomly entering code into MATLAB and had accidentally put a exclamation mark in the command window (I can't remember the exact reason why I was using exclamation marks). The result was quite interesting: it sent the command to the command interpreter outside of MATLAB which, of course, flagged this as an error. In seeing this, I stopped what I was doing and started to play about with this newly discovered shortcut.


My first command was !bash which, being in Linux, should run the BASH shell... which it did. I could then use MATLAB as a glorified terminal window. Whilst in BASH, I decided to check for updates for the machine which would require me to use sudo followed by the update command (and as I use Ubuntu, that's simply turns out to be sudo apt-get update). As always (with the exception of doing this in MATLAB), I was prompted for my password. Interestingly, MATLAB behaved like the terminal window and didn't display my password which normally happens should one Telnet their POP3 server (a hobby of mine). I guess this would be due to a so-called seamless "pipe" between MATLAB and, in this case, BASH.


From here, one can then go a step further an build m-files that run external commands. Of course, there's the issue of platform dependency here (mainly for the UNIX group) but one can work around this issue. A nice feature (at least in the UNIX version) is the fact that ! commands run in the current working directory (try !pwd and await a response) which means navigation isn't a problem. One minor niggle that I haven't been able to resolve just yet is trying to take a result from the external command without resulting to taking output manually using the command > output.log method.


So let's create something that works out what platform we're running on from within MATLAB and obtains the flavour of Linux using Linux command uname



if isunix

% In Unix, determine OS type

!uname -s > unixname.txt

% Now read this file:

fid = fopen(unixname.txt, 'rt');

if feof(fid) == 0 % Determine if at end of file

osname = fgetl(fid); % Get line

else

disp('File ended early');

end

elseif ispc

disp('I''m in MS Windows');

else

disp('Impossible: You''re not running either Windows or UNIX?');

end


Hopefully you are able to see the potential here in executing external applications based on what operating system environment is currently being used. Both Windows and UNIX support the command > output.log method (which you may remember from the Queue system). In fact, I frequently create script files using a combination of echo and the "append" verb >> where I can get away with it.


Read more on this article...

Tuesday, April 22, 2008

SSH client and X server for Windows

If, like me, you enjoy connecting/frequently connect remotely to either a cluster/grid computing system or even a UNIX/Linux box, you'll probably be familiar with the Secure Shell (or SSH) which is normally pre-installed in some shape or form.


Those on Windows have to install a third-party application unless you plan to remote desktop into a machine (which, might I add, is not the "best" option when connecting to grid computers, etc.). So I shall attempt to delve into the Win32 side of things for a change as some colleagues recently had a spot of trouble SSH-ing into the University Grid service, Iceberg.



I have already described one use for SSH although is by no means the only thing that can be done. SSH is useful in sending command-based instructions to and from a remote computer. If, however, you wish to do something outside a terminal/command window (besides leaving the computer), one can use an X server (also fondly referred to as X11). The X server is an application which can display some form of GUI (in the form of a window) by providing a framework to the operating system for drawing and moving windows (and most of us took this for granted!). Again, with UNIX/Linux system, an X server is normally packaged with the operating system (unless you're adamant in sticking with console/command based operating systems only).


Those that use (m)any of the distributions of Linux (and possibly UNIX) may know that SSH is mostly likely going to be from the OpenSSH group which distributes both client and server-side software. Likewise, the X server is provided by the X.org Foundation (which has a pretty predictable domain name making me wonder which came first) although it is possible that different operating systems use slightly customised versions of the X11 server (e.g. Apple (surprisingly)).


So let's go into a Windows frame of mind... we have no X server, no SSH and different commands. Whilst some would give up at this point, others will persevere abd reap what few benefits you'll get from this. Two applications spring to mind (again, there are many out there, but these are the two I'd recommend) and these are PuTTY (for SSH-ing) and Xming (which provides the X window for Windows). Xming also includes an adapted version of PuTTY which can lead to problems as I'll describe later (but not that far away in the distance). Putty provides Windows users with a means to SSH, rlogin (that's the remote login protocol) and telnet (although this option exists for many of the Windows installations) whilst allowing the connection to the configured (and, believe me, there are many options for the SSH user).


The other day, a friend asked whether it was possible to access Iceberg in Windows without having to install the Hummingbird suite (which requires a licence to be purchased). Knowing a few open source alternatives, I directed my friend to PuTTY and Xming. It turns out, they already had PuTTY and had customised the default settings to access one of the few Sun machines in the laboratory. We therefore proceeded to continue with the Xming installation (fonts inclusive) and set this up. This is when we began encountering problems as when we attempted to use SSH with the X window setup, we were unable to connect despite having entered the correct password numerous times. After a few attempts, I looked into how Xming works in our setup. The adapted version of PuTTY is what raised my suspicions and so investigated this area. I had suspected that the original (untouched) PuTTY was saving the default settings within Windows Registry and that the adapted version was also referring to the very same settings (so wasn't as altered as it perhaps could have been). To verify this, one would require access to the registry keys (oh joy). Luckily, this was quite simply...

HKEY_CURRENT_USER\Software\SimonTatham\PuTTY


I deleted the registry key/branch such that PuTTY was no longer in registry (at this point, I'd like to remind the read that Windows registry is quite a dangerous toy and should be used with care. Not taking care can result in temporary death of Windows and/or its applications which may or may not require result in its re-installation. The author(s) take no responsibility for themselves or the actions/inactions of the reader). The registry key can be recreated easily by simply running the PuTTY (obviously, this doesn't work should one wish to restore the entire contents of registry had one accidentally deleted the parent branch). From here, Xming could function without the need to constantly prompt for passwords. From this, I don't advise people to make changes to their default profiles within PuTTY; should they do, Xming is not guaranteed to work.


Read more on this article...

Sunday, February 3, 2008

Shiny Presentations with Beamer

In the not too distance past, I was asked to convert a set of notes for a taught module to a set of presentation slides. The work presentation has become synonymous with the words Microsoft PowerPoint within recent years and virtually every presentation you see or hear about is created using PowerPoint. Fortunately, there's a group of people (mostly academics or those that enjoy the challenge (read: have free time)) who prefer to use a (free) cross-platform solution to the presentation problem (everything to me is considered to be a problem) and that solution is: LaTeX.


With LaTeX, one is able to do a multitude of things but, on its own, is mainly restricted to nicely formatted documents without having the hassle of needing to position everything to your liking. Yes, formatting is automatic... to a large extent. There are times when its complex formatting algorithms get is slightly wrong, so you have to enter some commands to get it back on track. I won't go into great detail with the LaTeX side of things as there are many examples around that will illustrate its power to make a professional looking document look... professional.


Beamer is a LaTeX class that allows you to create a Beamer presentation. Whilst this description is pretty vague (and hasn't answered the question "what is beamer?", I can assure you that it can produce professional looking presentations with great ease. If, however, you're looking to replace PowerPoint completely with all the flashy transitions and effects (and you are one of the few that enjoy the peeling text and/or the typewriter effect), this may not be for you.


Additionally, a Beamer presentation is created as an Adobe PDF file. Whilst this may not entirely be a bad thing, for slide transitions and basic animations, Adobe Reader is really the only software available that will display these correctly. You will, otherwise, have to move frame-by-frame through each animation which is almost as bad as the typewriter effect in PowerPoint. The nice thing is that by having the output as a PDF file, most people will be able to view the presentation without having to pay for extremely expensive software. To view a Beamer presentation, neither LaTeX nor Beamer need be installed - only when creating the presentation are these packages needed. Like most good things, there is one downside: you have to install most of this yourself. Beamer does not normally come with LaTeX and if you use a Mac or a Windows machine (or even some *nix distributions), you will have to install the packages yourself. Luckily, there are some distributions of LaTeX that come complete with Beamer (which saves the hassle of installing it manually), so I suggest you read the TUG and Beamer resources for more information on what to do.


So on the assumption that you've managed to install both LaTeX and Beamer, we'll jump into the deep end by attempting to describe what everything does. Beamer, by default, has a few presentation styles which are predefined and, by no means, restricted. You can set up your own template with its own colour set once you're familiar with the layout and syntax of both LaTeX and Beamer. To get started, I'll firstly define a basic slide.



% For handouts (comment for slides)
%\documentclass[handout]{beamer}
%\usepackage{pgfpages}
%\pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm]
% End of handout section

% % For slides (comment for handouts)
\documentclass{beamer}
% % End of Slides section

\usepackage[english]{babel}
\usepackage[latin1]{inputenc}
%\usepackage{times}
%\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}

%\usetheme{Berlin}
\usetheme{UoS} % like Berlin

% % Sheffield Theme
% \usetheme{SheffieldCol}
% \renewcommand{\encodingdefault}{T1}
% \renewcommand{\rmdefault}{steph}
% \renewcommand{\sfdefault}{blake}
% \renewcommand{\ttdefault}{pcr}

\title{My First Presentation}
\subtitle{Chapter 1: How to use Beamer}
\author{Andrew Hills}\institute{The University of Sheffield}
\date{3\textsuperscript{rd} February 2008}

\begin{document}

\begin{frame}
\titlepage
\end{frame}

\begin{frame}
\frametitle{Outline}
\tableofcontents
\end{frame}

\section{Introduction}

%
% Motivation and Examples
%

\subsection{Motivation}

\begin{frame}
\frametitle{Motivation}
\begin{itemize}
\item Beamer presentations are in PDF format meaning that virtually anyone can view a presentation without the need to use a bloated office software package.
\item \LaTeX has the ability to create simple and effective documents which is inherited in the Beamer class.
\item Mathematical equations can be added simply without the need to use a third-party equation editor e.g. $\sum_{n=1}^6 \alpha_n + \frac{1}{n}\beta = \gamma$
\end{itemize}
\end{frame}

\end{document}


The header information opens by telling LaTeX what type of document we want to create which, in this case is beamer. We have the option to create handouts or slides - I've added both (but commented out one so it will compile) and can be switched easily. On the \usepackage{...} side, we can specify what font packages (if any) we wish to use. As an engineer, I write documents with some equations and, more frequently, linear algebra. As a result, I've added the American Mathematical Society's mathematics class and font package. The AMS package allows me to (easily) create nicely formatted matrices and vectors, providing I remember the command. We then continue with the normal LaTeX preamble which specifies the document's owner and the document's title.


Creating a slide is fairly easy. You declare that you want to start a new frame by typing \begin{frame}, declare a frame title by using the subtly named tag \frametitle{My Title}, write some words down before finally closing the frame with the code \end{frame}. Easy. A title frame can be created without much fuss by using the code \titlepage between a \begin{frame} and \end{frame}. Table of contents? No problem: declare a frame as before and then use the tag \tableofcontents. LaTeX does the rest.


So, at this point, you could create some basic slides and handouts. Pictures can be added using the tag \includegraphics[options]{loction/of/file.ext}. Options are optional (so don't include the square brackets if you don't wish to specify anything) or you can specify exact measurements such as height=4cm and LaTeX will do just that without deforming the image (i.e. it keeps the same aspect ratio). Additionally, should you create handouts, the slides are rescaled meaning that you don't have to alter any of the measurements in order to cater for the smaller area. Graphics should be of certain file types: PDF files work as do .png files (I'm certain JPEG will work too, but others won't be as forthcoming due to the nature of the PDFLaTeX compiler).


There are numerous templates available (as I mentioned earlier) which are pre-installed with the Beamer class (on *nix and on Macs, the files are stored within your LaTeX path under tex/latex/beamer/themes/; MS Windows-wise... I'm unsure but shouldn't be difficult to locate). Try \usetheme{Berlin} which, in fact, utilises the beamerthemeBerlin.sty file within the theme directory. The theme files specify which colour, inner and outer themes to apply. The colour theme is, as its name suggests will alter the colour settings of the template whereas the inner theme alters the internal layout such as bullet points. The outer theme dictates the overall layout of the slides, so if you want slide numbers and other miscellaneous information on all your slides (e.g. logos), then the outer style is what you'll need to edit. When I first started playing with these files, I copied them to my local directory (so as not to corrupt the original files (again)) and then renamed the themes such that they wouldn't interfere with the predefined template styles.


The easiest and, quite possibly, simplest edit is the colour scheme. As every business entity knows, a corporate colour scheme using garish and often clashing colours is the route to success. The University is no exception, so I set about altering the colour style sheet to match the University's colour scheme. With a palette of baby blues and hot pink, I was half way to converting the University endorsed PowerPoint template with a Beamer template (which would, of course, save the patience and perhaps lives of the few that prefer Beamer). Colours can be defines with a Style file using RGB controls: \definecolor{sheffieldblue}{rgb}{0.16, 0.1, 0.44} where each value is between 0 and 1 represents the elements red, green and blue respectively. Remember that HTML and other scripting languages normally require colour values between 00 and FF (that's 00 and 255 in decimal) for each RGB value.


Unfortunately, the master template can be difficult to edit if you want to dramatically redesign it. Your best bet is to try to find a template that is close to the end product (or as close as you can get to it) and then begin butchering it. It's what I did in order to get a slide that resembles the University's own (no matter how painful it looks). Patience and a lot of looking up TeX and LaTeX commands will help you along your way. By this point, you should have a good template that you can now use for your presentations. I mentioned transitions and animations earlier - none of which I have experimented with but I have seen them in action elsewhere. As a result, I won't go into details.


So what about fonts? Do you miss the Windows fonts that you were so used to seeing in Microsoft PowerPoint presentations? Luckily, there's plenty of information here... plus you'll have the added joy of facilitating ligatures (yes, I know, it's a link to Wikipedia *shudder*) and other typographical features. The fonts that are provided with LaTeX already have these features enabled by default. The problem with ligatures arises when you've imported a TTF (TrueType Font) into LaTeX. First thing's first, how do you import a TTF font? There's a utility called ttf2tfm which should exist on *nix and Mac LaTeX installations and should come with the MikTeX distribution for Windows users. You'll also need a copy of the T1 encoding file T1-WGL4.enc which you can get from either Rakityansky's or Cheong's site. This file is basically a font descriptor file for the TrueType font layout. You will need to then run the command:



ttf2tfm times.ttf -q -T T1-WGL4.enc -v ectimes.vpl rectimes.tfm >> timesfont.map

ttf2tfm timesi.ttf -q -T T1-WGL4.enc -v ectimesi.vpl rectimesi.tfm >> timesfont.map

ttf2tfm timesbd.ttf -q -T T1-WGL4.enc -v ectimesbd.vpl rectimesbd.tfm >> timesfont.map

ttf2tfm timesbi.ttf -q -T T1-WGL4.enc -v ectimesbi.vpl rectimesbi.tfm >> timesfont.map


For the above example, it takes Times New Roman's normal, italics, bold and bold & italics TTF files and parses them individually. For some fonts, you may only have the one TTF file. Rakityansky details how to create slanted versions of the Times New Roman font, so check that out if you wish to do just that. At this point, you have the font files which can be used as they are, but currently are ligature-less. Despite the ligatures existing in the converted TFM file (providing the TTF file had the ligatures), the ttf2tfm utility did not generate the necessary associations so we will have to manually add them (oh joy). To do this, we need to convert the TFM file to a .pl file using the tftopl utility which is used as follows:


tftopl rectimes.tfm rectimes.pl


The resulting .pl file will contain a list of ligature and character information which all seems quite strange at first. As pointed out by Cheong, it's simply a case of adding some lines along the lines of (pardon the pun):



(LIGTABLE
(LABEL C f)
(LIG C i D 28)
(STOP)
(LABEL C -)
(LIG C - D 21)
(STOP)
(LABEL D 21)
(LIG C - D 22)
(STOP))


This is read as follows:



Line 2: If the character starts with "f"
Line 3: The next character is "i", use character number 28 (in decimal) (which should be the ligature for the combined fi - check the character font map of the TTF file)
Line 3:No more for this character
Line 4: If the character starts with "-"
Line 5: And the next character is also a "-", use the character number 21 (in decimal) which should give an en-dash
Line 6: No more for this ligature
Line 7: If the character is decimal 21 (i.e. line 5)
Line 8: And the proceeding character is a "-", use character number 22 (which should be em-dash).


For a font with more ligatures, you may be able to define ff, ffl and ffi:



(LIGTABLE
(LABEL C f)
(LIG C i D 28)
(LIG C f D 27)
(LIG C l D 29)
(STOP)
(LABEL D 27)
(LIG C l D 31)
(LIG C i D 30)
(STOP)
(LABEL C -)
(LIG C - D 21)
(STOP)
(LABEL D 21)
(LIG C - D 22)
(STOP))


Check that you have these ligatures first otherwise whenever one types ffl or ffi in a document, it'll be replaced with a professional looking blank space. So once you've got your .pl file sorted out, you can recompile it back into the TFM file it once was. To do this, use the utility pltotf which is used as follows:


pltotf rectimes.pl rectimes.tfm


Whilst you could try using your new fonts, you may find that it won't work correctly as LaTeX will try and look for a font descriptor file (they have .fd as their file extension0. The font descriptor file tells LaTeX which fonts to use depending on the setting. In some cases, it may not be possible to define a font in particular condition (e.g. slanting), so by telling LaTeX to use a substitute, the text will still display without any major issues. For every font style, you specify a {n}ormal, {sl}anted and {it}alics definition. This can be done to each of the categories bold type {b}, normal {m} and bold-extended {bx}. The file descriptor file can look like:



\ProvidesFile{t1MyRomanFont.fd}[Description of font]

\DeclareFontFamily{T1}{MyRomanFont}{}

\DeclareFontShape{T1}{MyRomanFont}{m}{n}{<-> rectimes}{}

\DeclareFontShape{T1}{MyRomanFont}{bx}{n}{<-> rectimesbd}{}

\DeclareFontShape{T1}{MyRomanFont}{m}{it}{<-> rectimesi}{}

\pdfmapline{+rectimes\space <times.ttf\space><t1-wgl4.enc} space=""><timesbd.ttf\space><t1-wgl4.enc} space=""><timesi.ttf\space><t1-wgl4.enc}></t1-wgl4.enc}></timesi.ttf\space>


At this point, you can use your fonts by using any one of two methods (possibly more, but these should suffice). The first is to renew either the RM (Roman) or SF (Serif) font set used by LaTeX by adding the following code to the header of your LaTeX file:



\renewcommand{\encodingdefault}{T1}
\renewcommand{\rmdefault}{MyRomanFont}
\renewcommand{\sfdefault}{MySerifFont}


Whereas the second method is to simply call the font as and when you require it using something like:



\DeclareTextFontCommand{\textmysf}
{\fontencoding{T1}\fontfamily{MySerifFont}\selectfont}
\DeclareTextFontCommand{\textmyrm}
{\fontencoding{T1}\fontfamily{MyRomanFont}\selectfont}


Again, this should go in the preamble of your document (i.e. before \begin{document} but after the \documentclass{...} tag).


You've got a lot of files now that were used in the transfer process of TTF to TFM. Unfortunately, some of these will have to remain. The TTF file will have to stay (as will the TFM file). I didn't go into detail as to what the .map file did but will now briefly mention it: the .map file pieces the italics, bold and italics & bold fonts together so when you request any of these options, LaTeX will automatically use the correct font. This is in addition to your font descriptor file .fd as described earlier.


Whilst this will work, the fonts will be compiled as bitmaps which aren't particularly useful. Rakityansky describes that one can create Adobe Font Metrics to overcome this problem. I won't go regurgitate what he has already written (as I doubt I can do a better job). If you do use this method, you'll notice that new TFM files will be created (which is something we already have), so this will mean you'll have to redefine the ligatures should you decide to overwrite the files.


Cleaning up


As I mentioned earlier, at this point, you'll probably have lots of files. Some of these have to stay, but other don't. Those that stay should be the Font Descriptor files (.fd), the encoding file (T1-WGL4.enc), the TrueType Font file (.ttf), the font metric file (.tfm), the Adobe Font Metric File (if you proceeded down this route) (.afm) and the font MAP files (.map). The rest can go... with the exception of any LaTeX document, of course!


Read more on this article...

Saturday, February 2, 2008

MATLAB Licence Management

For those that enjoy altering MATLAB licence files and/or working remotely, this may be of interest to you. Providing you have access to your workplace’s VPN server and that the IT department haven’t locked the network down, you may be able to run MATLAB remotely.

As described by MathWorks, your licence file header may contain a line in the form:

SERVER Licence_Server_Name HostID TCP_PortNumber

When using your (company/institution’s) computer remotely, you are likely going to be on a different domain name regardless of whether you are connected via VPN or not. Consequently, if you are having problems opening MATLAB despite using VPN in the hope it will connect to the licence server, you may wish to check your licence file header before assuming it’s a firewall/network configuration issue.

You can find the licence files (named “license.dat” - take note of the spelling) within your MATLAB directory ($MATLAB).

For Windows installations: $MATLAB\bin\win32\license.dat

For Unix installations: $MATLAB/etc/license.dat

Now, before one starts altering these licence files, consider backing up the original files.

Open the license.dat file within the text editor of your choice and check the form of the header as mentioned above. Should the Licence_Server_Name field not be of the form server.domain.com or server.domain.co.uk but is instead stated as server, this will at least be partly, if not entirely, responsible for the licence issues you are having.

By simply stating the licence server as server, the local computer appends its local domain name and extension (as set by the network configuration) to the server name in the belief that the licence server is connected to your local network. By appending your workplace’s domain and domain extension (i.e. .com, .co.uk, etc.) to the licence server name, the local computer will attempt to connect to the server under your workplace’s domain.

Once you have made changes to your licence file, save it, establish a connection through VPN and then fire up MATLAB. This should solve any issues where two or more networks try to connect to one licence server that is connected to only one of those networks (where each network has a different domain name).

Should this not have solved your problems, it’s possible that the IT department have either used IP filtering (to limit the range in IP addresses that connect to the server) or have invested in a firewall that blocks the port TCP_PortNumber.

So, in a nutshell (and for those that enjoy procedural steps):

  1. Backup your license.dat file
  2. Open your license.dat file in a text editor
  3. Ensure that a domain is indeed specified within the header where the header is in the following form:
    SERVER Licence_Server_Name HostID TCP_PortNumber
  4. If the Licence_Server_Name has a (valid) domain and extension, the problem appears to be caused by a firewall or IP filtering.
    If the Licence_Server_Name doesn’t have a domain and extension, add one (preferably your workplace domain and domain extension) such that the header now reads:
    SERVER Licence_Server_Name.Domain.Ext HostID TCP_PortNumber
  5. Save any changes made
  6. Establish a VPN connection, load MATLAB and await results
  7. If it is either hanging or you have receiving error messages about the licence manager, it’s possible that the domain and domain extension entered is incorrect. Otherwise, the IT department have a firewall or IP filter that’s blocking your licence request.

Another thing to bear in mind is that you should, in theory, keep your VPN connection open whilst you are using MATLAB. By doing so, any other licence requests can be sent to the licence server (such as checking out and checking in toolboxes) when required without causing any further grief. Hence, you should really only disconnect from VPN once MATLAB has closed.

Read more on this article...