Mar 5 / Richard Knop

Zend Framework IE conditional stylesheets and scripts

You can append a conditional stylesheet in a controller action like this:

  1. $this->view->headLink()->appendStylesheet('/path/to/some/styles.css', 'screen', 'IE 8');

Which would produce this markup:

<!--[if IE 8]> <link href="/path/to/some/styles.css" media="screen" rel="stylesheet" type="text/css" /><![endif]-->

Adding a conditional JavaScript file is similar:

  1. $this->view->headScript()->appendFile('/path/to/some/script.js', 'text/javascript', array('conditional' => 'IE'));

And that would produce markup like this:

<!--[if IE]> <script type="text/javascript" src="/path/to/some/script.js"></script><![endif]-->

It can pretty useful when you need to use IE conditional comments just for a single controller action. Otherwise, if it applies site-wide, it’s better to just put conditional stylesheets and/or scripts in a layout file.

Mar 1 / Richard Knop

A Simple Search Engine Implementing Zend_Search_Lucene

Zend_Search_Lucene is a PHP port of a popular Java search engine Apache Lucene. It is also an important part of Zend Framework. Some say that it is too sluggish to be used in robust web applications and recommend faster alternatives such as Sphinx but that is not today’s topic. In this post I will show you a basic implementation of Zend_Search_Lucene that has worked well so far for medium websites I have worked on. There are two main tasks you will have to take care of:

  1. Creating an index and updating it regularly.
  2. Searching the index with a powerful query language.

First, let’s create a fresh search index. I know it’s already tiresome but I will use a simple blog application for my example implementation. To simplify it even further, it will only be possible to search blog posts. The posts schema looks like this:

CREATE TABLE posts (
id INT NOT NULL AUTO_INCREMENT,
title VARCHAR(255) NOT NULL,
body TEXT NOT NULL,
created_at DATETIME NOT NULL,
updated_at DATETIME NOT NULL,
user_id INT NOT NULL,
INDEX (created_at),
INDEX (user_id),
FOREIGN KEY (user_id)
REFERENCES users(id)
ON UPDATE CASCADE
ON DELETE CASCADE,
PRIMARY KEY (id)
) ENGINE = INNODB;

Creating the search index is easy:

  1. Zend_Search_Lucene::setDefaultSearchField('contents');
  2.  
  3. // create blog posts index located in /data/posts_index
  4. // make sure the folder is writable
  5. $index = Zend_Search_Lucene::create('data/posts_index');
  6.  
  7. // $this->_getTable() is a method that returns a model
  8. // get() method of the model returns all posts from the database
  9. $posts = $this->_getTable('Posts')->get();
  10. // iterate through posts and build the index
  11. foreach ($posts as $p) {
  12.     $doc = new Zend_Search_Lucene_Document();
  13.     $doc->addField(Zend_Search_Lucene_Field::UnIndexed('entry_id', $p->id));
  14.     $doc->addField(Zend_Search_Lucene_Field::Keyword('title', $p->title));
  15.     $doc->addField(Zend_Search_Lucene_Field::UnStored('contents', $p->body));
  16.     $index->addDocument($doc);
  17. }
  18. // commit the index
  19. $index->commit();

Pretty straightforward. You can see I have used three different static methods for adding fields to the document:

  • UnIndexed: unindexed and unstored (therefor unsearchable) but they are returned with search results. Unindexed fields usually store primary keys, timestamps or file paths.
  • Text: indexed, stored and tokenized. Text fields are searchable and are returned with search hits. Titles, first and last names, cities and states, post codes and street names are all good candidates for keyword fields.
  • UnStored: indexed and unstored – ideal for large texts.

There are more types of fields you can use (keyword, binary) but you can read about them in the documentation.

Next thing you need to do is update the index every once in a while so the search hits return up-to-date information. There are two ways to get around this problem. The most obvious is to update the index every time a new post is published or an existing post is edited. Another approach would be to set up a cron job to run every now and then and rebuild the index. Which way you choose depends on many variables such as expected index size (a very large index can have few GBs in size).

Secondly, the index is already taken care of, so let’s search it:

  1. Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8());
  2.  
  3. Zend_Search_Lucene::setResultSetLimit(10);
  4.  
  5. // explode the search query to individual words
  6. $words  = explode(' ', urldecode($request->getParam('search_for')));
  7. // start a search query and add a term for each word to it
  8. $query = new Zend_Search_Lucene_Search_Query_MultiTerm();
  9. foreach ($words as $w) {
  10.     $query->addTerm(new Zend_Search_Lucene_Index_Term($w), true);
  11. }
  12.  
  13. // open and query the index
  14. $index = Zend_Search_Lucene::open('data/posts_index');
  15. $results = $index->find($query); // the search results

That was possibly the simplest possible example of a Lucene search query. You can, however, create very complex queries with the powerful Lucene query language. You can either build queries manually in PHP or you can use Zend_Search_Lucene methods to build them. It’s so easy a baby could do it.

To search for posts with words ‘hello’ and ‘word’ in the contents field you would write this query:

hello

To search for a post that must contain ‘hello’ and may contain ‘world’:

+hello world

To search for a post that must contain ‘hello’ in the contents field and may contain ‘world’ in the title field:

+hello title:"world"

And those were just basics. You can use boolean operators, wildcards, ranges and even perform a fuzzy search.

Feb 8 / Richard Knop

http_build_query() in PHP5

I can’t tell how many times I need to pass some data via GET method to another PHP script and I always forget about this neat little function so I just do it like this (like a fool):

  1. $arr = array('foo' => 'bar',
  2.              'baz' => 'boom',
  3.              'cow' => 'milk');
  4. $query = array();
  5. foreach ($arr as $k => $v) {
  6.     $query[] = $k . '=' . $v;
  7. }
  8. $query = implode('&', $query);
  9. // foo=bar&baz=boom&cow=milk

When it’s so easy with the http_build_query() function:

  1. $query = http_build_query($arr);
  2. // foo=bar&baz=boom&cow=milk

This is also useful when using cURL to perform POST or GET calls.

Jan 9 / Richard Knop

Poderosa: great alternative to putty

Putty is the most popular telnet/ssh client and most of you are probably using it. However, there’s one major problem I have with putty – it doesn’t remember username and password. Everytime I want to login through ssh to my server, I have to find a secret paper with my username and password (very complex and impossible to remember -  it’s randomly generated by this password generator). And that’s quite a drag after a while.

So I looked for an alternative and found Pedorosa. It’s more complicated than putty (plus you have to install it) but it remembers your username and password plus it has many more features you might find interesting such as multiple connections in tabs and so on.

Jan 3 / Richard Knop

Please use partials in your ZF apps

Wouldn’t you agree that using exactly the same markup in 15 views is a little bit redundant? Now imagine doing that 10 times in your application (that’s 15*10-15 which equals to 135 reduntant repetitions of the same piece of XHTML). Well, that’s the easier part. Now imagine you need to make few changes in the markup after few days. Guess what – you are going to do those worthless 135 repetitions again. And surely you might need more changes in the markup two weeks later… I could go on. There’s nothing easier than avoiding this problem in Zend Framework.

The solution is to use partial view helper which allows you to put the repetitive XHTML into a single file. For example, this could be your partial:

  1. <?php // saved in views/scripts/index/partials/partial.phtml ?>
  2. <h3><?php echo $this->escape($this->heading); ?></h3>
  3. <p>
  4.     Lorem ipsum dolor sit amet, consectetur adipiscing elit.
  5.     Mauris porta, sapien at accumsan venenatis, neque velit
  6.     sodales augue, sit amet posuere odio enim quis risus.
  7.     Aliquam semper, ipsum porttitor ultricies bibendum, sem
  8.     sapien tincidunt purus, eget sollicitudin felis eros
  9.     fermentum felis. Praesent vehicula varius est at semper.
  10.     Nam nulla augue, fermentum in varius vel, tincidunt non
  11.     nibh. In vitae neque vitae metus dignissim hendrerit.
  12.     Nulla eget massa nec sapien tempor ultricies a et tellus.
  13.     Sed imperdiet, risus quis vehicula ornare, justo lorem
  14.     iaculis sapien, id semper massa nisi eu purus.
  15.     Suspendisse sed urna eu risus rhoncus bibendum. Cras
  16.     feugiat consectetur nisi id dictum. Pellentesque quis
  17.     mauris a risus dapibus dictum nec sed nisl.
  18. </p>
  19. <ul>
  20.     <li>List Item 1: <?php echo $this->escape($this->li1); ?></li>
  21.     <li>List Item 2: <?php echo $this->escape($this->li2); ?></li>
  22.     <li>List Item 3: <?php echo $this->escape($this->li3); ?></li>
  23. </ul>

And this is how you would include it in all views where the same piece of markup occurs:

  1. <?php echo $this->partial('index/partials/partial.phtml', array(
  2.     'heading' => 'Lorem Ipsum',
  3.     'li1' => 'Hello',
  4.     'li2' => 'World',
  5.     'li3' => '!!!')); ?>

It’s really easy and it saves a huge amount of time both to you and to a developer that might work on your application in the future so please, all of you Zend Framework developers, use partial helpers wherever there is a repetitive XHTML markup in your application.

Dec 22 / Richard Knop

Making Zend_Captcha_Image easier to read

Sometimes people will complain that the captcha is too difficult to read and they have to reload the page in order to be able to pass the captcha test. If you are not willing to say goodbye to the very flexible Zend_Captcha_Image there are only two ways how to make it easier to read:

  1. Use different font (I have found that Arial is sometimes hard to read, a good replacement seems to be LBRITED.TTF).
  2. Set lower dot and line noise level.

You can use “dotNoiseLevel” and “lineNoiseLevel” to tune down the captcha noise (and surpisingly this is not mentioned in the Zend Framework documentation):

  1. $captcha = new Zend_Form_Element_Captcha('captcha', array(
  2.     'label' => 'Captcha',
  3.     'helper' => null,
  4.     'captcha' => array(
  5.         'captcha' => 'Image',
  6.         'wordLen' => 5,
  7.         'timeout' => 300, // 5 minutes timeout
  8.         'font' => 'fonts/LBRITED.TTF',
  9.         'fontSize' => 20,
  10.         'width' => 100,
  11.         'height' => 60,
  12.         'imgDir' => 'images/captcha/',
  13.         'imgUrl' => '/images/captcha/',
  14.         'dotNoiseLevel' => 15,
  15.         'lineNoiseLevel' => 1
  16.     )
  17. ));

Above I went over the top perhaps. Nevertheless, you must be able to find a compromise so that the users will be happy and the security side of your website won’t suffer too much.

Dec 21 / Richard Knop

Caching with Zend_Cache

Caching with PHP is the best way to speed up your application and it’s quite easy thanks to native PHP modules such as APC or output buffering control. It’s even much much easier when you are using the Zend Framework as it contains a native class just for caching – Zend_Cache. To use the Zend_Cache you must first initialize it in the bootstrap and add it to the registry so you can access it easily in controllers, I do it like this:

  1. protected function _initCache()
  2. {
  3.     $frontend = array('lifetime' => 7200,
  4.                      'automatic_seralization' => true);
  5.     // where are we going to stored the cached files?
  6.     $backend = array('cache_dir' => 'cache');
  7.     $this->cache = Zend_Cache::factory('core',
  8.                                        'File',
  9.                                        $frontend,
  10.                                        $backend);
  11. }
  12.  
  13. protected function _initRegistry()
  14. {
  15.     $this->registry = Zend_Registry::getInstance();
  16.     $this->registry->cache = $this->cache;
  17.     // I store much more in the registry, of course
  18.     // for instance, configuration and db adapter
  19. }

The frontend and the backend adapters can take much more arguments, read the documentation to learn all possible options. I initialized the Zend_Cache object with the factory method. First argument means that we want to use the Zend_Cache_Core frontend which is the core of the Zend_Cache module. The second argument is the backend we want to use the Zend_Cache_Backend_File backend or that we want to store cached data as files in a specific directory. The other two parameters are frontend and backend options.

Now we can easily access the cache object in controllers or models:

  1. $cache = Zend_Registry::get('cache');
  2. // does the cache contain data we are looking for?
  3. if (!$result = $cache->load('myUniqueId')) {
  4.     // if not let's cache the data
  5.     // here I use only a random array but in a real application
  6.     // you would probably cache some database entries
  7.     $data = array('John Doe', 'Jane Doe', 'Baby Doe');
  8.     // besides unique cache id you can use tags to categorize data
  9.     $cache->save($data, 'myUniqueId', array('tag1', 'tag2'));
  10. } else {
  11.     // dump the cached data
  12.     var_dump($result);
  13. }

That was easy, wasn’t it?

Cleaning the cache

You will surely also want to remove cached files at some point in your application:

  1. // clean all cached files
  2. $cache->clean(Zend_Cache:: CLEANING_MODE_ALL);
  3. // clean only outdated cached files
  4. $cache->clean(Zend_Cache::CLEANING_MODE_OLD);
  5. // remove a particular cache id
  6. $cache->remove('myUniqueId');
  7. // remove records tagged as "tag1" AND "tag2"
  8. $cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG,
  9.               array('tag1', 'tag2'));
  10. // remove records tagged as "tag1" OR :tag2"
  11. $cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG,
  12.               array('tag1', 'tag2'));
  13. // remove records NOT tagged as "tag1" or "tag2"
  14. $cache->clean(Zend_Cache::CLEANING_MODE_NOT_MATCHING_ANY_TAG,
  15.               array('tag1', 'tag2'));

That’s it for today and, by the way, Merry Christmas :)

Dec 7 / Richard Knop

500 Internal Server Error

I have recently switched web hosting provider and had to move my personal website and blog over to the new one. I have encountered a simple problem when trying to get my personal website to work (it’s a Zend Framework application). I got the 500 Internal Server Error:

500 Internal Server Error

The problem lied in this .htaccess rule:

php_flag magic_quotes_gpc off

So I just commented the line and the error went away.

The line’s purpose was to make sure magic quotes are turned off in PHP.  You can use it if your web host supports it, if not just add this to your bootstrap file:

  1. protected function _initGetRidOfMagicQuotes()
  2. {
  3.     if (get_magic_quotes_gpc()) {
  4.         function stripslashes_deep($value) {
  5.             $value = is_array($value) ?
  6.             array_map('stripslashes_deep', $value) :
  7.             stripslashes($value);
  8.             return $value;
  9.         }
  10.  
  11.         $_POST = array_map('stripslashes_deep', $_POST);
  12.         $_GET = array_map('stripslashes_deep', $_GET);
  13.         $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
  14.         $_REQUEST = array_map('stripslashes_deep', $_REQUEST);
  15.     }
  16. }
Nov 8 / Richard Knop

Perceptron in ANSI C

One of my school assignments was to write a simple ANSI C Perceptron algorithm that would be able to separate points on a two dimensional plane into two sets (-1 and 1). Fortunately, while reading a Wikipedia article about Perceptron, I have found a great external link at the bottom of it: C# implementation of a Perceptron. This helped me a lot to understand how the Perceptron works and how to implement it programatically.

What I have done is rewrite the code snippet from John Wakefield in the C language. Instead of data type double I used two integer arrays (one denotes x and the other one y coordinates of points from a training set). Plus I have added a simple code do draw a nice graph of the training set and its linear separation. I used GD to draw the image. I would also like to thank Amro for helping me finish this algorithm.

Perceptron linear separation

Here’s the entire code:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <gd.h>
  4. #include <math.h>
  5.  
  6. #define NUMEL            208
  7. #define LEARNING_RATE    0.1
  8. #define MAX_ITERATION    100
  9.  
  10. float randomFloat()
  11. {
  12.     float r = (float)rand() / (float)RAND_MAX;
  13.     return r;
  14. }
  15.  
  16. int calculateOutput(float weights[], float x, float y)
  17. {
  18.     float sum = x * weights[0] + y * weights[1] + weights[2];
  19.     return (sum >= 0) ? 1 : -1;
  20. }
  21.  
  22. int main(int argc, char *argv[])
  23. {
  24.     srand(time(NULL));
  25.  
  26.     float x[NUMEL], y[NUMEL], weights[3], localError, globalError, a, b;
  27.     int outputs[NUMEL], patternCount, i, p, iteration, output;
  28.  
  29.     FILE *fp;
  30.     if ((fp = fopen("training-set.txt", "r")) == NULL)
  31.     {
  32.         printf("Cannot open file.\n");
  33.         exit(1);
  34.     }
  35.     i = 0;
  36.     while (fscanf(fp, "%f %f %d", &x[i], &y[i], &outputs[i]) != EOF)
  37.     {
  38.         if (outputs[i] == 0)
  39.         {
  40.             outputs[i] = -1;
  41.         }
  42.         printf("%.4f %.4f %d\n", x[i], y[i], outputs[i]);
  43.         i++;
  44.     }
  45.     patternCount = i;
  46.  
  47.     system("PAUSE");
  48.  
  49.     weights[0] = randomFloat();
  50.     weights[1] = randomFloat();
  51.     weights[2] = randomFloat();
  52.  
  53.     iteration = 0;
  54.     do {
  55.  
  56.         iteration++;
  57.         globalError = 0;
  58.         for (p = 0; p < patternCount; p++)
  59.         {
  60.             output = calculateOutput(weights, x[p], y[p]);
  61.  
  62.             localError = outputs[p] - output;
  63.  
  64.             // Update weights.
  65.             weights[0] += LEARNING_RATE * localError * x[p];
  66.             weights[1] += LEARNING_RATE * localError * y[p];
  67.             weights[2] += LEARNING_RATE * localError;
  68.  
  69.             globalError += (localError * localError);
  70.         }
  71.  
  72.         /* Root Mean Squared Error */
  73.         printf("Iteration %d : RMSE = %.4f\n", iteration,
  74.                sqrt(globalError / patternCount));
  75.  
  76.     } while (globalError != 0 && iteration <= MAX_ITERATION);
  77.  
  78.     // Display network generalisation.
  79.     printf("X       Y     Output\n");
  80.     float j, k;
  81.     for (j = -1; j <= 1; j += .5)
  82.     {
  83.         for (j = -1; j <= 1; j += .5)
  84.         {
  85.             // Calculate output.
  86.             int output = calculateOutput(weights, j, k);
  87.             printf("%.4f %.4f %s\n", j, k, (output == 1) ? "Blue" : "Red");
  88.         }
  89.     }
  90.  
  91.     // Display modified weights.
  92.     printf("Modified weights: %.2f %.2f\n", weights[0], weights[1]);
  93.  
  94.     // Create image representation.
  95.     gdImagePtr im;
  96.     im = gdImageCreateTrueColor(600, 600);
  97.     if (im != 0)
  98.     {
  99.          // Allocate colors.
  100.          int white = gdImageColorAllocate(im, 255, 255, 255);
  101.          int lightGrey = gdImageColorAllocate(im, 220, 220, 220);
  102.          int black = gdImageColorAllocate(im, 0, 0, 0);
  103.          int blue = gdImageColorAllocate(im, 0, 0, 255);
  104.          int red = gdImageColorAllocate(im, 255, 0, 0);
  105.          int green = gdImageColorAllocate(im, 0, 200, 50);
  106.  
  107.          // White flood fill.
  108.          gdImageFill(im, 0, 0, lightGrey);
  109.  
  110.          // Points.
  111.          float cx, cy;
  112.          for (i = 0; i < patternCount; i++)
  113.          {
  114.              // Calculate output.
  115.              int output = calculateOutput(weights, x[i], y[i]);
  116.  
  117.              cx = floor(300 + 30*x[i] + 0.5);
  118.              cy = floor(300 - 30*y[i] + 0.5);
  119.  
  120.              int color = (output == 1) ? blue : red;
  121.  
  122.              gdImageFilledEllipse(im, (int)cx, (int)cy, 5, 5, color);
  123.          }
  124.  
  125.          // Linear separation
  126.          a = -weights[0] / weights[1];
  127.          b = -weights[2] / weights[1];
  128.          printf("Decision boundary (line) equation: y = %.4fx + %.4f\n", a, b);
  129.          // x = -10 => y = -10a+b
  130.          // x = 10 => y = 10*a + b
  131.  
  132.          gdImageLine(im, 0, (int)(300 + 300*a - 30*b), 600, (300 - 300*a - 30*b), green);
  133.  
  134.          // X coordinate.
  135.          gdImageLine(im, 0, 300, 600, 300, black);
  136.  
  137.          // Y coordinate.
  138.          gdImageLine(im, 300, 0, 300, 600, black);
  139.  
  140.          fp = fopen("training.png", "wb");
  141.          if (fp != 0)
  142.          {
  143.              gdImagePng(im, fp);
  144.              fclose(fp);
  145.          }
  146.     }
  147.     gdImageDestroy(im);
  148.  
  149.     system("PAUSE");
  150.     return 0;
  151. }

I used the Dev C++ to write the program. Here is how to use GD in Dev C++.

#include <stdio.h>
#include <stdlib.h>
#include <gd.h>
#include <math.h>

float randomFloat()
{
srand(time(NULL));
float r = (float)rand() / (float)RAND_MAX;
return r;
}

int calculateOutput(float weights[], float x, float y)
{
float sum = x * weights[0] + y * weights[1];
return (sum >= 0) ? 1 : -1;
}

int main(int argc, char *argv[])
{
// X coordinates of the training set.
float x[] = {
-3.2, 1.1, 2.7, -1
};

// Y coordinates of the training set.
float y[] = {
1.5, 3.3, 5.12, 2.1
};

// The training set outputs.
int outputs[] = {
1, -1, -1, 1
};

int patternCount = sizeof(x) / sizeof(int);

float weights[2];
weights[0] = randomFloat();
weights[1] = randomFloat();

float learningRate = 0.01;

int iteration = 0;
int i, p;
float globalError;

do {

globalError = 0;
int p = 0; // iterator
for (p = 0; p < patternCount; p++)
{
// Calculate output.
int output = calculateOutput(weights, x[p], y[p]);

// Calculate error.
float localError = outputs[p] – output;

if (localError != 0)
{
// Update weights.
for (i = 0; i < 2; i++)
{
float add = learningRate * localError;
if (i == 0)
{
add *= x[p];
}
else if (i == 1)
{
add *= y[p];
}
weights[i] += add;
}
}

// Convert error to absolute value.
globalError += fabs(localError);

printf(“Iteration %d Error %5.2f\n”, iteration, globalError);

iteration++;
}

} while (globalError != 0);

// Display network generalisation.
printf(“X Y Output\n”);
float j, k;
for (j = -1; j <= 1; j += .5)
{
for (j = -1; j <= 1; j += .5)
{
// Calculate output.
int output = calculateOutput(weights, j, k);
printf(“%5.2f %5.2f %s\n”, j, k, (output == 1) ? “Blue” : “Red”);
}
}

// Display modified weights.
printf(“Modified weights: %5.2f %5.2f\n”, weights[0], weights[1]);

// Create image representation.
gdImagePtr im;
FILE *fp;
im = gdImageCreateTrueColor(600, 600);
if (im != 0)
{
// So the points are further from each other
// and the graph is more readable.
int multiplier = 50;

// Allocate colors.
int white = gdImageColorAllocate(im, 255, 255, 255);
int lightGrey = gdImageColorAllocate(im, 220, 220, 220);
int black = gdImageColorAllocate(im, 0, 0, 0);
int blue = gdImageColorAllocate(im, 0, 0, 255);
int red = gdImageColorAllocate(im, 255, 0, 0);
int green = gdImageColorAllocate(im, 0, 200, 50);

// White flood fill.
gdImageFill(im, 0, 0, lightGrey);

// Points.
float cx, cy;
for (i = 0; i < patternCount; i++)
{
// Calculate output.
int output = calculateOutput(weights, x[i], y[i]);

cx = floor(300 + multiplier*x[i] + 0.5);
cy = floor(300 – multiplier*y[i] + 0.5);

int color = (output == 1) ? blue : red;

gdImageFilledEllipse(im, (int)cx, (int)cy, 10, 10, color);
}

// Linear separation.
float a = 0, b = 0;
for (i = 0; i < patternCount; i++)
{
int fx = (a > 0 && b > 0) ? 1 : 0;
a += learningRate * (y[i] – fx) * x[i];
b += learningRate * (y[i] – fx);
}
printf(“y = %5.2fx + %5.2f\n”, a, b);
// x = -300 => y = -300*a + b
// x = 300 => y = 300*a + b
gdImageLine(im, -2, (int)(300 + multiplier*300*a + b), 598, (int)(300 – multiplier*300*a + b), green);
gdImageLine(im, -1, (int)(300 + multiplier*300*a + b), 599, 300 – (int)(multiplier*300*a + b), green);
gdImageLine(im, 0, (int)(300 + multiplier*300*a + b), 600, 300 – (int)(multiplier*300*a + b), green);
gdImageLine(im, 1, (int)(300 + multiplier*300*a + b), 601, 300 – (int)(multiplier*300*a + b), green);
gdImageLine(im, 2, (int)(300 + multiplier*300*a + b), 602, 300 – (int)(multiplier*300*a + b), green);

// X coordinate.
gdImageLine(im, 0, 300, 600, 300, black);

// Y coordinate.
gdImageLine(im, 300, 0, 300, 600, black);

fp = fopen(“training.png”, “wb”);
if (fp != 0)
{
gdImagePng(im, fp);
fclose(fp);
}
}
gdImageDestroy(im);

system(“PAUSE”);
return 0;
}

Oct 26 / Richard Knop

Clear command line, variables and command history in MATLAB

Here are three useful MATLAB commands.

To clear the command line:

clc

To clear all variables:

clear all

To clear the command history:

com.mathworks.mlservices.MLCommandHistoryServices.removeAll

Just putting this information down for my own reference so I won’t have to search for it later.