Monday, March 21, 2016

What do I need to start coding?

Source code is the human-readable version of our program. A constant across programming languages is that source code is just plain text (the text you type is the only content of the file you create). Yes, you can write whatever program  you want by using the notepad application that is included in your computer. If you want to ease your work as programmer, then you may want to use a more specialized software such as a programmer text editor, or an Integrated Development Environment (IDE).

Source code files (commonly referred to as source files) have many different name extensions depending on the programming  files. For example, files whose name end with ".c" are files with C code, files with ".py" extension are used for Python programs, ".js" files contain Javascript code, and ".m" files are MATLAB scripts. Although they are used for different programming languages, they are all plain text files.

Writing source-code using a rich-text editor or a word processor may not work very well. The file that results after saving a text in one of these editors may contain much more information than your text itself. For example, let's write the words "Hello world!" (and make it bold, with size of 20 points, and blue), visualize it in WordPad, and in a plain text editor (notepad).


Observe how the plain text editor "sees" much more content than just the text we typed. A similar thing would happen with any other word processor.

So what the source-code to machine-code would really see, is the version we observe in the notepad. That version may not make any sense to such translator. Therefore, if we want to really know what we are going to pass, we better use a plain text editor.

Programmer text editors are also plain text editors. They just have some extra features added to them. Maybe the most popular ones are line numbering (displaying the line number at the left of the text), and syntax high-lighting (highlighting the special keywords from our programming language with different colors so the code becomes easier to read and navigate).



Integrated Development Environments (IDEs) are a set of programs that have the common purpose of helping us develop our programs. Some examples are:
 - Visual Studio,
 - Eclipse,
 - Code-Blocks,
 - Net-Beans,
 - Wing IDE,
 - Among many, many others...

All IDEs include a plain text editor. In addition, they may also include a project manager, which is used to navigate through our software project files easier than using a regular file explorer. They may have also an integrated shell for us to input commands or displaying the output of our program. There are infinite kinds of tools that may be included in an IDE, but these are the most common.



At the end, deciding what tool to use depends mostly on how comfortable you feel using it. Some people may prefer regular text editors over IDEs, or vice-versa. Most of the tools are free or have a trial version, so you can check what fits your needs and preferences.

PD: I prefer dark backgrounds as they tend to be more confortable to my eye by minimizing the amount of light that the screen emits.

Tuesday, March 8, 2016

Running Matlab from inside Python

I have been programming in Python since I was working for Intel. I am amazed on how friendly is its syntax and how few lines of code can do many things. Later, it was exciting to know that Mathworks (the company that develops Matlab) decided to make its powerful software to be natively compatible with Python. Now it is possible to combine the easy Python syntax with the mathematical power inside Matlab and exchange data between the two (or at least it has become very easy).

This Matlab Engine for Python is available from Matlab 2014b. To install it just open a command shell (it MUST be run as administrator to work properly), and type:

    cd %MATLAB_PATH%\%MATLAB_VERSION%\extern\engines\python
    %PYTHON_PATH%\python.exe setup.py install

The %MATLAB_PATH% variable points to the directory where we have installed Matlab. %MATLAB_VERSION% is, as it says, the Matlab version to use. And %PYTHON_PATH% is the directory where have Python installed. In my case, these variables have the values:

    %MATLAB_PATH% = C:\Program Files\MATLAB
    %MATLAB_VERSION% = R2016a
    %PYTHON_PATH% = C:\Python27

Actually, without defining variables, in my machine it looks like this:



If the "error" word appears after executing these commands, check out to see what is missing. Most of the times the error log has enough information to know the next step for fixing the issue.

Once the engine has been installed, it is ready to go. Let's now run an example to plot a sine wave in Matlab, from Python. Open a Python shell and write...

    import matlab
    eng = matlab.engine.start_matlab()
    x = eng.linspace(0.0,5.0,1000.0)
    y = eng.sin(x)
    eng.figure(nargout=0)
    eng.hold("on",nargout=0)
    eng.box("on",nargout=0)
    eng.line(x, y, nargout=0)

When finished, you should see a Matlab graph pop out. With this, we have enabled the Matlab Engine for Python.



I highly recommend you to check these examples in Matlab's website:

- Install MATLAB Engine API for Python
Install MATLAB Engine API for Python in Nondefault Locations
Start and Stop MATLAB Engine for Python
Connect Python to Running MATLAB Session
Call MATLAB Functions from Python
Call MATLAB Functions Asynchronously from Python
Call User Script and Function from Python
Redirect Standard Output and Error to Python
Use MATLAB Engine Workspace in Python
Use MATLAB Handle Objects in Python
Use MATLAB Arrays in Python
Sort and Plot MATLAB Data from Python
Get Help for MATLAB Functions from Python

And now, let's have fun with a whole world of possibilities!

What are programming languages?

So we got an algorithm or process we pretend to program. What is next? We have to write it in such a way that the hardware understands it (so it can run it!). Here we have a problem: the human-language is not understandable by machines (not by themselves). We need some kind of an interface between the machine and us: a programming language. In the background, programs are always translated to machine code. Such translation is done an auxiliary program.



There are many programming languages out there: Java, C, Pascal, Fortran, Octave, and so on. However, only few of them are popular (used frequently). However, the popularity of each programming language changes with time. Good news are that, once you learn one of them, learning another becomes easier; the third one even easier, and etcetera. They all follow certain patterns: they all have a way to handle variables, for example.

1: Java; 2: C; 3: C++; 4: Python; 5: C#; 6: &; 7: PHP; 8: Javascript; 9: Ruby; 10: Matlab
The 10 most popular programming languages in 2015, by IEEE Spectrum.

But why don't we program in machine-language directly? The problem with the machine language is that it is so hard to read and write by humans that it becomes unpractical for coding. Most hardware systems only understand specific combinations of ones and zeroes (binary code), and that is what machine language is. Moreover, the machine language changes with the processor model. Then a program that has been written in machine-language may not work on different machines!

Some programs are easier to translate to machine-code, and therefore, they are harder to understand by humans. Such languages are called "low-level" programming languages. On the other hand we have "high-level" languages, that are easy to understand by humans, but more difficult to translate to machine code.

Programs written in low-level languages tend to run faster than programs written in high-level languages. However, high-level programs tend to run independently of what hardware executes it, while the low level programs should be re-translated or re-coded to run on different machines. But at the end, choosing a programming language or another depends on the application and how it gets impacted by the language characteristics.