Tag Archives: Programming

Assigning ASC-II Characters in MATLAB

Recently I have been working to replicate the cockpit of Piper PAS 28 aircraft in MATLAB. Although the things went pretty well, but I was having serious trouble to define interface such that the user can control the instruments from keyboard. After some lengthy search and experiments with the programme I managed to assign my instruments with keyboard commands. I thought it is now a best idea to share this with others who are trying something along the same lines. It worked well for me, so I hope it will work with you too.

The graphic model includes the commands defining the user interface, allowing user to control the simulator from keyboard. The user interface model incorporates two files, first file ‘GETKEY’, waits for a key press and returns the ASCII code. Accepts all ASC-II characters, including backspace (8), space (32), and enter (13), etc, that can be typed on the keyboard. Non-ASC-II keys (ctrl, alt, ..) return a NaN. The example of GETKEY function is illustrated below:

function ch = GETKEY(m)
global Display
fprintf(‘\nPress any key: ‘) ;
ch = getkey ;
fprintf(‘%c\n’,ch) ;
fprintf(‘\nPress the Ctrl-key: ‘) ;
if strcmp(getkey(‘non-ascii’),’control’),
fprintf(‘OK\n’) ;
else
fprintf(‘ … wrong key …\n’) ;
end

In order to assign the keys to variables and appropriate figure, Display structures and MATLAB graphics library as discussed earlier are extensively used. The graphic handles assigned in sub-modules of graphic library are used to allocate the key commands. This is best illustrated by the following example:

% Determine the callback string to use
if nargin == 1,
if strcmp(lower(m),’non-ascii’),
callstr = [‘set(gcbf,”Userdata”,get(gcbf,”Currentkey”)) ;
uiresume ‘] ;
else
error(‘Argument should be the string ”non-ascii”’) ;
end
else
callstr = [‘set(gcbf,”Userdata”,double(get(gcbf,”Currentcharacter”))) ; uiresume ‘] ;
end

% Set up the figure
fh = figure(‘keypressfcn’,callstr, … % using Handle defined
‘windowstyle’,’modal’,…
‘position’,[0 0 1 1],…
Name’,’GETKEY’, …
‘userdata’,’timeout’) ;
try

% Wait for something to happen
uiwait ;
ch = get(fh,’Userdata’) ;
if isempty(ch),
ch = NaN ;
end
catch% error return to empty matrix.
ch = [] ;
end

Hope you guys got the idea. Happy Programming and please do not hesitate to add or comment about the article

Advertisements

Leave a comment

Filed under Engineering

Make Files under linux (C and Fortran)

Hello People,

Just to extend our discussion on C code compilation under, I would briefly go through the make file procedure in Linux. In software development, make is a utility that automatically builds executable programs and libraries from source code by reading files called Makefiles which specify how to derive the target program. Though its primary use is as described above, make is not restricted to just creating executable programs from source files. Any process that involves transforming a dependency file to a target result (by executing some number of arbitrary commands) is applicable to make.

GNU make is frequently used in conjunction with the GNU build system. Its departures from traditional make are most noticeable in pattern-matching in dependency graphs and build targets, as well as a number of functions which may be invoked to have the make utility do things like collect a list of all files in the current directory.

Using the same code we developed earlier, HelloWorld.c, We will now run the code, using the MakeFile in terminal. The nature of these files depends on the program structure and what is required of it, I will however, briefly go through the simple and basic Make File routine to compile and run the C code called ‘HelloWorld.c’, The above file has the following ‘MakeFile’ (spaces are TABS to not spaces, so don’t copy and paste)

all: HelloWorld

HelloWorld: HelloWorld.o

HelloWorld.o: HelloWorld.c

clean:
rm -f HelloWorld HelloWorld.o

.PHONY: clean

Use $make –help to see the help options in terminal, Once written, run the following command to obtain the executable:

$ make -f MakeFile

If the Make file is written in a proper way, it should work. Hope that brief intro about make files was useful, as it took me a lot of time to understand and get the basics of Make File 🙂 I will go through the detail of the Make File modeling for both C and Fortran later. To wrap up things:

NAME
make – GNU make utility to maintain groups of programs
SYNOPSIS
make [ -f makefile ] [ option ] … target …
WARNING

This man page is an extract of the documentation of GNU make . It is updated only occasionally, because the GNU project does not use nroff. For complete, current documentation, refer to the Info file make.info which is made from the Texinfo source file make.texinfo.

Normally you should call your makefile either makefile or Makefile. The first name checked, GNUmakefile, is not recommended for most makefiles. You should use this name if you have a makefile that is specific to GNU make, and will not be understood by other versions of make. If makefile is `-’, the standard input is read.

make updates a target if it depends on prerequisite files that have been modified since the target was last modified, or if the target does not exist.

Leave a comment

Filed under Engineering