In VisualStudio to indent all code in the file you are currently working on:
1. Select all code: CTRL+A
2. Choose CTRL+K, CTRL+D
That's it!
My Tips and Tricks for C#, C, C++, MatLAB, Java, LaTeX, Python and more!
Thursday, November 16, 2017
Wednesday, November 15, 2017
Notepad++: Remove all lines containing a certain word/sentence/regex
In Notepad++ (a wonderful Program!) you can easily delete all LINES containing a certain word/sentence or regex.
It's basically a two step process.
You need to find all lines that containing your search string.
Now from the Notepad++ Menus, choose:
Search -> Bookmark -> Remove Bookmarked Lines
That's it!
It's basically a two step process.
You need to find all lines that containing your search string.
- Open the search window (CTRL+F)
- Choose the Mark tab
- Insert the string you want to search for
- Select the Bookmark line
- Press Mark All.
Now from the Notepad++ Menus, choose:
Search -> Bookmark -> Remove Bookmarked Lines
That's it!
Sunday, November 12, 2017
C#: Setting up a connection with PostgreSQL in VisualStudio
In this small tutorial I gonna show you how to setup a connection to your local PostgreSQL instance and write/read data to/from a table (in the following I will call this table my_test_table).
You will need:
CREATE TABLE public.my_table
(
id integer NOT NULL,
extref integer NOT NULL,
val numeric NOT NULL,
CONSTRAINT my_table_pkey PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
);
ALTER TABLE public.my_table
OWNER TO postgres;
Install Npgsql from the Package Manager Console in VisualStudio:
Tools -> NuGet Package Manager -> Package Manager Console
In the command prompt type:
Install-Package Npgsql -Version 3.2.5 (or whatever new version is available here)
In VisualStudio create a simple console application and use this code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Npgsql;
namespace TestDB
{
class Program
{
static public void TestDB()
{
// Adapt for your configuration (port, Username, password etc)
var connString = "Host=localhost;Port=5432;Username=postgres;Password=postgres;Database=postgres";
using (var conn = new NpgsqlConnection(connString))
{
conn.Open();
// Insert some data
using (var cmd = new NpgsqlCommand())
{
cmd.Connection = conn;
cmd.CommandText = "INSERT INTO my_table (id, extref, val) VALUES (@id, @extref, @val)";
cmd.Parameters.AddWithValue("id", 10);
cmd.Parameters.AddWithValue("extref", 10);
cmd.Parameters.AddWithValue("val", 150);
cmd.ExecuteNonQuery();
}
// Retrieve all rows
Console.WriteLine("Reading from the DB table...");
using (var cmd = new NpgsqlCommand("SELECT * FROM my_test_table", conn))
using (var reader = cmd.ExecuteReader())
while (reader.Read())
{
string readLine = string.Format("id={0}, extref={1}, value={2}", reader.GetString(0), reader.GetString(1), reader.GetString(2));
Console.WriteLine(readLine);
}
}
}
static void Main(string[] args)
{
TestDB();
Console.ReadKey();
}
}
}
NOTE
If you try to run this program twice, you will get an exception because you will be trying to insert the same id.. twice and this is not possible because ID is a unique key.
So remember to delete the table's content each time you runt this application.
The list of available parameters for the connection string is available here
References:
http://www.npgsql.org/doc/index.html
You will need:
- Visual Studio Community www.visualstudio.com/
- PostgreSQL for Windows: www.enterprisedb.com (be sure to select pgAdmin3 during the installation, it will make your life easier)
- The Npgsql package: www.npgsql.org
CREATE TABLE public.my_table
(
id integer NOT NULL,
extref integer NOT NULL,
val numeric NOT NULL,
CONSTRAINT my_table_pkey PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
);
ALTER TABLE public.my_table
OWNER TO postgres;
Install Npgsql from the Package Manager Console in VisualStudio:
Tools -> NuGet Package Manager -> Package Manager Console
In the command prompt type:
Install-Package Npgsql -Version 3.2.5 (or whatever new version is available here)
In VisualStudio create a simple console application and use this code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Npgsql;
namespace TestDB
{
class Program
{
static public void TestDB()
{
// Adapt for your configuration (port, Username, password etc)
var connString = "Host=localhost;Port=5432;Username=postgres;Password=postgres;Database=postgres";
using (var conn = new NpgsqlConnection(connString))
{
conn.Open();
// Insert some data
using (var cmd = new NpgsqlCommand())
{
cmd.Connection = conn;
cmd.CommandText = "INSERT INTO my_table (id, extref, val) VALUES (@id, @extref, @val)";
cmd.Parameters.AddWithValue("id", 10);
cmd.Parameters.AddWithValue("extref", 10);
cmd.Parameters.AddWithValue("val", 150);
cmd.ExecuteNonQuery();
}
// Retrieve all rows
Console.WriteLine("Reading from the DB table...");
using (var cmd = new NpgsqlCommand("SELECT * FROM my_test_table", conn))
using (var reader = cmd.ExecuteReader())
while (reader.Read())
{
string readLine = string.Format("id={0}, extref={1}, value={2}", reader.GetString(0), reader.GetString(1), reader.GetString(2));
Console.WriteLine(readLine);
}
}
}
static void Main(string[] args)
{
TestDB();
Console.ReadKey();
}
}
}
NOTE
If you try to run this program twice, you will get an exception because you will be trying to insert the same id.. twice and this is not possible because ID is a unique key.
So remember to delete the table's content each time you runt this application.
The list of available parameters for the connection string is available here
References:
http://www.npgsql.org/doc/index.html
Tuesday, August 29, 2017
How the I2C protocol works
I found this interesting video explaining how the I2C or two-wire interface works.
Since this is a very popular protocol, you will meet soon or later in the embedded systems, so it's extremely worth to learn it!
http://howtomechatronics.com/tutorials/arduino/how-i2c-communication-works-and-how-to-use-it-with-arduino/
Enjoy!
Since this is a very popular protocol, you will meet soon or later in the embedded systems, so it's extremely worth to learn it!
http://howtomechatronics.com/tutorials/arduino/how-i2c-communication-works-and-how-to-use-it-with-arduino/
Enjoy!
Tuesday, June 20, 2017
Appunti di Informatica Libera [pdf]
Ecco qui il link al PDF di Appunti di Informatica Libera (precedentemente noti come Appunti Linux). L'ultimo aggiornamento รจ del 11.11.2013.
Opera monumentale!
ftp://na.mirror.garr.it/mirrors/appuntilinux/2013.11.11/a2-2013.11.11.pdf
http://a2.pluto.it/a2/a0.pdf
Altri formati:
ftp://na.mirror.garr.it/mirrors/appuntilinux/2013.11.11/
Online
Opera monumentale!
ftp://na.mirror.garr.it/mirrors/appuntilinux/2013.11.11/a2-2013.11.11.pdf
http://a2.pluto.it/a2/a0.pdf
Altri formati:
ftp://na.mirror.garr.it/mirrors/appuntilinux/2013.11.11/
Online
Sunday, May 28, 2017
tic and toc functions in C++ (millisecond resolution)
This is a newer version of my original post about Matlab-like tic/toc functions in C++
These new versions of the TIC/TOC functions have millisecond resolution while the old ones rounded the time difference to the seconds.
Here is the the new tic_toc.h header:
#ifndef TIC_TOC_H
#define TIC_TOC_H
#include <iostream>
#include <chrono>
typedef std::chrono::high_resolution_clock Clock;
typedef std::chrono::milliseconds milliseconds;
static Clock::time_point t0 = Clock::now();
void tic()
{
t0 = Clock::now();
}
void toc()
{
Clock::time_point t1 = Clock::now();
milliseconds ms = std::chrono::duration_cast<milliseconds>(t1 - t0);
std::cout <<"Elapsed time is "<< ms.count() << " milliseconds\n";
}
#endif
// happy coding and performance testing :)
These new versions of the TIC/TOC functions have millisecond resolution while the old ones rounded the time difference to the seconds.
Here is the the new tic_toc.h header:
#ifndef TIC_TOC_H
#define TIC_TOC_H
#include <iostream>
#include <chrono>
typedef std::chrono::high_resolution_clock Clock;
typedef std::chrono::milliseconds milliseconds;
static Clock::time_point t0 = Clock::now();
void tic()
{
t0 = Clock::now();
}
void toc()
{
Clock::time_point t1 = Clock::now();
milliseconds ms = std::chrono::duration_cast<milliseconds>(t1 - t0);
std::cout <<"Elapsed time is "<< ms.count() << " milliseconds\n";
}
#endif
// happy coding and performance testing :)
Labels:
C++,
chrono,
howto,
matlab,
milliseconds,
performance,
tic,
toc
Thursday, April 13, 2017
Undefined functions while using struct and optim packages in Octave
If you have encountered errors like these in octave:
error: 'cell2fields' undefined near line 939 column 11
error: called from
__nonlin_residmin__> at line -1 column -1
__lm_svd__ at line 191 column 9
__nonlin_residmin__ at line 1128 column 21
nonlin_curvefit at line 83 column 18
nlinfit at line 169 column 18
OR these
error: '__collect_constraints__' undefined near line 152 column 7
error: called from
__nonlin_residmin__ at line 151 column 48
nonlin_curvefit at line 83 column 18
nlinfit at line 169 column 18
mean that NOT all the functions in the packages struct or optim have been correctly imported into Octave.
istread of importing the single folders in the Octave's path:
addpath('/home/eddie/octave/struct-1.0.14') % NOT ENOUGH!!!
addpath('/home/eddie/octave/optim-1.5.2') % NOT ENOUGH!!!
just use the pkg command:
pkg load struct ;
pkg load optim ;
That's it. Happy scientific programming :)
error: 'cell2fields' undefined near line 939 column 11
error: called from
__nonlin_residmin__> at line -1 column -1
__lm_svd__ at line 191 column 9
__nonlin_residmin__ at line 1128 column 21
nonlin_curvefit at line 83 column 18
nlinfit at line 169 column 18
OR these
error: '__collect_constraints__' undefined near line 152 column 7
error: called from
__nonlin_residmin__ at line 151 column 48
nonlin_curvefit at line 83 column 18
nlinfit at line 169 column 18
mean that NOT all the functions in the packages struct or optim have been correctly imported into Octave.
istread of importing the single folders in the Octave's path:
addpath('/home/eddie/octave/struct-1.0.14') % NOT ENOUGH!!!
addpath('/home/eddie/octave/optim-1.5.2') % NOT ENOUGH!!!
just use the pkg command:
pkg load struct ;
pkg load optim ;
That's it. Happy scientific programming :)
Subscribe to:
Posts (Atom)