C scanf with comma separated values.

Unlike scanf function family in MATLAB, C scanf doesn’t consider the comma(,) as a delimiter for the specifier “%s.”

Let’s say we have a data file as follows.

Spiderman,Parker,99
Superman,Ken,89
Batman,Wayne,79

The following code will not work. The variable “first_name” will have the whole line “Spiderman,Parker,99″ since it thinks there is no delimiter in the line.

char first_name[50], last_name[50];
int grade;
FILE *fp = fopen("data.csv", "r");
fscanf(fp, "%s,%s,%d\n", first_name, last_name, &grade);

Here is a way to make scanf functions think the comma (,) is a delimiter.

fscanf(fp, "%[^,],%[^,],%d\n", first_name, last_name, &grade);

fscanf in MATLAB

Unlike in C/C++, fscanf in MATLAB repeats the scanning as long as the formatted text matches. It is convenient but also confusing sometimes.

For example, in a text file there are lines as follows.

Image Width Height: 800, 600
Red X Y Radius: 200, 300, 300
Green X Y Radius: 300, 400, 200
Blue X Y Radius: 500, 500, 150

We can read the first line using

dim = fscanf(fid, '%*s %*s %*s %d, %d\n');             (a)

A problem arises after this scanning since it tries to scan the next line as well with the format that is given. So even if you try to read the next line with the fscanf (b), the scanning will fail since the fscanf in (a) tries to scan the next line.

redc = fscanf(fid, '%*s %*s %*s %*s %d, %d, %d\n');    (b)

So in this specific case, it is better to use the followings.

dim  = fscanf(fid, 'Image Width Height: %d, %d\n');
redc = fscanf(fid, 'Red X Y Radius: %d, %d, %d\n');
grnc = fscanf(fid, 'Green X Y Radius: %d, %d, %d\n');
bluc = fscanf(fid, 'Blue X Y Radius: %d, %d, %d\n');

There are better ways but this would be quick fix.

No Console Output in Eclipse with CDT in Windows

In Windows the Console doesn’t display any output from printf. Let’s see an example.

int main(void) 
{
    int i;

    printf("Enter a number: ");
    scanf("%d", &i);
    printf("You entered %d\n", i);
    return EXIT_SUCCESS;
}

The text “Enter a number: ” is not shown in the Console windows of Eclipse with CDT. This is due to the fact all the stdout and stderr ouputs in Windows are buffered. So we can simply flush stdout and stderr everytime you use printf. But it is not practical solution. You may add these two lines of code in the first part of your main function.

  • setvbuf(stdout, NULL, _IONBF, 0);
  • setvbuf(stderr, NULL, _IONBF, 0);

These lines enforce stdout/stderr not to have buffers which means that all the output will be happening as soon as it takes place. One serious problem of this solution is that the program will be crashed if you start the debugger and try to step over any of printfs. So in the Debug mode, starting a new command prompt console is a good idea. Let’s me summary this workaround.

Use setvbuf for Run mode

Put this function in your main function in the first place.

void set_std_buffer_off()
{
    setvbuf(stdout, NULL, _IONBF, 0);
    setvbuf(stderr, NULL, _IONBF, 0);
}
int main(void)
{
    // turn off the std buffering
    set_std_buffer_off();

    // start your own code.
    ....
}

You now see outputs from printfs in your code when you run the program.

Debug mode

For the debug mode, use GDB command file.

Create a text file and named it as “gdbinit.txt” The file name is not important anyway. You may use any proper name. Open the text file and put this single line into the file and save it.

set new-console on

This line simply means that new console will be started when the debugger starts a debugging session.

Open “Preferences” (Window > Preferences). Set the text file for “GDB command file:” in C/C++ –> Debug –> GDB. Click the “Apply” button and the “OK” button.

Try to debug your program. Then you will see a command prompt when the debugger starts. All outputs and inputs will be happening in the new command prompt without having any troubles.

Using Eclipse with CDT in Windows

Unlike in Mac OS X, it is harder to install Eclipse with CDT in Windows than it looks. This is partially because the Windows OS does not have Java Runtime Environment as default settings. Another problem is that there are two versions, 32-bit and 64-bit, under the same name of the OS. x64-base processors can have either 32-bit OS or 64-bit OS.

Windows 32-bit or 64-bit?

First thing first. Make sure that what system type of Windows you are using. If your PC is pretty new, most likely you have 64-bit Windows. If not, it could be 32- or 64-bit. One simple way to find out which version you are using is to check the “Program Files” folder name. If your system has “Program Files (x86)” along with “Program Files” then your OS is for 64-bit. You may open Control Panel to see the version. The “System type:” in “System and Security->System” tells 64-bit OS or 32-bit OS. Again using x64-bit based processor does not guarantee that the OS type is 64-bit.

Java Runtime Environment

Eclipse is an executable program written in Java and built for Java. Java executables need Java Virtual Machine. Java Runtime Environment or JRE is a basic package to run Java programs. There are several “Editions” in Java. Java SE (Standard Edition) is good for Java applications on desktops and servers. You may find Java “Development” Kit, or JDK while you are googling Java SE Download. This is for Java Developers and includes JRE. We do not need this version because we just want to run a Java program, Eclipse. Download one “jre” according to your OS type. x64  means it is for Windows 64-bit OS. x86 is for Windows 32-bit OS. Install it. Clear check boxes when the Java Setup asks to install something along with Java unless you really really really want to install the toolbar for your Web browsers.

MinGW

The next step is to install Minimalists for GNU for Windows or MinGW. This is for someone who wants to use the GNU Compiler Collection to develop native Windows applications without installing Cygwin (a Unix-like environment and command line interface for Windows). Use an installer to install necessary packages. The installer, mingw-get-setup.exe, can be downloaded from http://sourceforge.net/projects/mingw/files/Installer/ Run the installer and select two packages: mingw32-base and mingw-gcc-g++. Then select the menu item, “Apply Changes” in the “Installation” menu.

Eclipse CDT

Find a Eclipse package with the latest CDT at http://www.eclipse.org/cdt/downloads.php. And again download one according to your OS type. There are Eclipses with CDT for Windows 32-bit and 64-bit.

Project – Hello World ANSI C Project

Select “New – C Project.” Put a name in “Project name:” And select “MinGW GCC” as your “Toolchains:” The Toolchain is a set of programming tools. And we are using MinGW GCC as the toolchain.

Build the Project Manually at First

The newly generated project must be built manually at least once before you run the program. Go to and select the menu “Project – Build Project.”

That’s it.

Get PhoneGap to Work on Mac OS X

PhoneGap is a free open source framework that allows us to create mobile apps using standard web technologies such as HTML, CSS, and JavaScript (http://phonegap.com).

If you follow the steps in the web page, Install PhoneGap (http://phonegap.com/install/), most likely you would get stuck in the second or third step. This is because the documentation is horrible and gives us a lot of confusions.

Earlier versions of PhoneGap were tightly integrated into Eclipse and Xcode. But it is obvious that PhoneGap stops catching up new updates of various platform. It seems that they went back to command line tools with poor quality documentations.

It makes me think that the only reason why Adobe purchased this company was just to kill this product.

Anyway, let us get started. To install 3.3 version of PhoneGap, Node.JS is required.

1. Install Node.JS

Go to http://nodejs.org and find the INSTALL button. Click it to install. This step is very straightforward and easy thanks to the node.js install program.

2. Install PhoneGap

Open “Terminal” and run the following.

$ sudo npm install -g phonegap

phonegap is a command line tool. You will be able to use “phone gap” command to create a project. But you will NOT be able to build or run the app in any platform.

3. Create a Project

$ phonegap create my-app
$ cd my-app

This will create a phone gap project but it doesn’t include any platform dependent modules.

4. Install Cordova

There are buzzes regarding to PhoneGap, Cordova, and their relationship. Here is a clarification from PhoneGap Blog. http://phonegap.com/2012/03/19/phonegap-cordova-and-what’s-in-a-name/

To make the long story short,

PhoneGap is a distribution of Apache Cordova. You can think of Apache Cordova as the engine that powers PhoneGap, similar to how WebKit is the engine that powers Chrome or Safari.

And,

If your goal is to build cross platform apps with HTML, JS and CSS then keep on using PhoneGap for everything you need. This isn’t to say the transition has been perfect. We messed up the last release by not correctly updating the documentation to reflect the name change which confused new users.

The conclusion is that we NEED cordova installation along with PhoneGap. Very weird.  Right? Anyway use the following command.

$ sudo npm install -g cordova

5. Add Platforms to a Project

The folder “platforms” inside “my-app” (the newly created project folder) has nothing from your creation. We have to add platforms (such as android and iOS). This is the moment where we have to use cordova. Let’s add the iOS platform that means cordova creates all necessary files to be a proper project for Xcode.

$ cordova platform add ios

Next let’s add the android platform.

$ cordova platform add android

You will see an error saying that ‘ant’ is not installed if you have installed it already in your system. Also I assume that you do not have MacPorts as well just like me. Let’s use “homebrew” to install “ant.”

$ ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go/install)"

After homebrew is successfully installed, use followings.

$ brew install ant

Now you are suppose to be able to install “android” platform. Try this again.

$ cordova platform add android

Now you can find two folders (ios and android) in the “platforms” folder.

6. Build Projects

Finally you are ready to build projects.

$ phonegap run android

This will compile, build, and install the project onto the device. If no Android device is connected to the system, it will start the Android emulator.

In  the same manner, use following to build and run the project for iOS.

$ phonegap run ios

You will see an error that says ios-sim was not found even though you have installed Xcode and by which your system has iOS Simulator already.

$ sudo npm install -g ios-sim

If you prefer using IDEs, you may import the android project from ADT and start the Xcode project file on Xcode.

iPhone Mockup

This is a simple online iPhone app mockup tool.

Useful link for research skills

A few days ago, one of my former colleagues from Texas A&M shared an interesting video featured by Simon PJ from Microsoft Research. The video was about how to write a good paper. It was very useful. So I googled him and found more materials from his web page. http://research.microsoft.com/en-us/people/simonpj/

The page has links about how to write a good research paper, give a good research talks, and write a good grant proposal.

http://research.microsoft.com/en-us/um/people/simonpj/papers/giving-a-talk/giving-a-talk.htm

 

 

Media coverage

My project among others funded by an NSF grant was introduced on the Flint Journal, a local newspaper. It is not 100% accurate descriptions about me and the project. One example is that it was not 10 years ago when I studied at Texas A&M University.

Kettering University receives half a million dollars in National Science Foundation grants

This one is on the school news coverage. Again the title is not. It is NOT “MRI” research. The grant name is “MRI” which stands for Major Research Instrument.

NSF grant will support high resolution MRI research

ABC12 news

Kettering University receives grants from National Science Foundation

Pull-up Resistor

Floating

See the picture [1] below. When the switch is connected, the input pin will get the high level. But what if the switch is not connected. It is hard to tell the input pin will be read as high or low. Also the value of the input pin is very much susceptible by noise. This phenomenon is referred as floating.

Pull-up Resistor

A resistor (usually more than 10KΩ. See the picture [2] below) can guarantee that the value of the input pin is high when the switch is disconnected.

For more details, reading [2] is recommended.

GM Foundation Funding

Computer Engineering Summer Day Camp is one of pre-college programs that GM Foundation decides to fund. The summer camp started from 2011 and has been successful.

Follow

Get every new post delivered to your Inbox.

Join 147 other followers