C/C++ program template with one CLI argument

This example program provides an example of a CLI executable in C/C++ that uses one command line argument and exits with a usage message if that argument is not present:

#include <stdio.h>

int main(int argc, char **argv)
{
    if(argc <= 1) { // <= (number of expected CLI arguments)
        fprintf(stderr, "Usage: %s <input file>\n", argv[0]);
        return -1;
    }
    
    // TODO Your code goes here!
    printf("Input file: %s\n", argv[1]);
    return 0;
}

Compile with

g++ -o cli-onearg cli-onearg.cpp

Usage example:

$ ./cli-onearg  
Usage: ./cli-onearg <input file>
$ ./cli-onearg my-input-file.txt
Input file: my-input-file.txt

 

Posted by Uli Köhler in C/C++

How to fix /usr/bin/ld: cannot find -lzlib

Problem:

You want to compile a C++ executable that uses zlib, but you see an error message like

/usr/bin/ld: cannot find -lzlib
collect2: error: ld returned 1 exit status

Solution:

Use -lz instead of -lzlib. The zlib library is named libz.so, not libzlib.so!

Example:

g++ -o ztest zpipe.c -lz

 

Posted by Uli Köhler in C/C++

FFT frequency resolution calculator

TechOverflow calculators:
You can enter values with SI suffixes like 12.2m (equivalent to 0.012) or 14k (14000) or 32u (0.000032).
The results are calculated while you type and shown directly below the calculator, so there is no need to press return or click on a Calculate button. Just make sure that all inputs are green by entering valid values.

points

Hz


f_{resolution} = \frac{f_{samplerate}}{N_{FFT}}

Posted by Uli Köhler in Calculators, Data science

Downloading PubChem raw data using rsync

PubChem raw data is hosted on the NCBI servers which provide convenient access using rsync.

Download PubChem data using a rsync command like

rsync -Pav ftp.ncbi.nlm.nih.gov::pubchem/Compound/CURRENT-Full/SDF/\*.gz pubchem/

This example command will download all current compounds in SDF format.

You can explore the PubChem directory structure by accessing the NCBI FTP server using your browser.

Posted by Uli Köhler in Bioinformatics

Hz to RPM converter

TechOverflow calculators:
You can enter values with SI suffixes like 12.2m (equivalent to 0.012) or 14k (14000) or 32u (0.000032).
The results are calculated while you type and shown directly below the calculator, so there is no need to press return or click on a Calculate button. Just make sure that all inputs are green by entering valid values.

Hz


f_{RPM} = f_{Hz} \cdot 60

Posted by Uli Köhler in Calculators

RPM to Hz converter

TechOverflow calculators:
You can enter values with SI suffixes like 12.2m (equivalent to 0.012) or 14k (14000) or 32u (0.000032).
The results are calculated while you type and shown directly below the calculator, so there is no need to press return or click on a Calculate button. Just make sure that all inputs are green by entering valid values.

RPM


f_{Hz} = \frac{f_{RPM}}{60}

Posted by Uli Köhler in Calculators

How to fill matrix with NaN in Octave

Also see: How to initialize NaN matrix in octave

If you have a matrix in Octave like

A = zeros(2,4)

you can fill it with NaN using

A(:,:) = NaN

Note that you have to use A(:) = NaN for 1D-matrices. A(:,:) = NaN for 2D matrices, A(:,:,:) = NaN for 3D matrices and so on.

Example:

>> A = zeros(2,4)
A =

   0   0   0   0
   0   0   0   0

>> A(:,:) = NaN
A =

   NaN   NaN   NaN   NaN
   NaN   NaN   NaN   NaN

 

Posted by Uli Köhler in Octave

How to initialize NaN matrix in octave

Also see: How to fill matrix with NaN in Octave

You can use NaN(N,M) as a function to initialize a NxM matrix filled with NaN in octave. For example, to initialize a 2x4x3 matrix, use

A = NaN(2,4,3)

Example:

>> A = NaN(2,4,3)
A =

ans(:,:,1) =

   NaN   NaN   NaN   NaN
   NaN   NaN   NaN   NaN

ans(:,:,2) =

   NaN   NaN   NaN   NaN
   NaN   NaN   NaN   NaN

ans(:,:,3) =

   NaN   NaN   NaN   NaN
   NaN   NaN   NaN   NaN
Posted by Uli Köhler in Octave

Simple body surface area calculator

Note about medical information:
This information is presented for informational purposes only and is intended for professionals. While we strive to provide accurate information, this information might be outdated, unintentionally misleading or incorrect. Consult a medical professional and/or read the primary sources cited in our article before basing any decision on this information.
TechOverflow calculators:
You can enter values with SI suffixes like 12.2m (equivalent to 0.012) or 14k (14000) or 32u (0.000032).
The results are calculated while you type and shown directly below the calculator, so there is no need to press return or click on a Calculate button. Just make sure that all inputs are green by entering valid values.

m

kg





 

Sources & Formulae: Halls.md

Posted by Uli Köhler in Calculators

How to remove all event listeners from a DOM element in Javascript

You can remove all event listeners from a DOM element in Javascript by replacing the element with a deep clone of itself. elem.cloneNode(...) will not clone the event listeners of the source element.

elem.replaceWith(elem.cloneNode(true));

Full example:

var elem = document.getElementById('mybutton');
elem.replaceWith(elem.cloneNode(true));

Source for the original (partial) suggestion on StackOverflow: @Felix Kling

Posted by Uli Köhler in Javascript

How to remove dashicons CSS from WordPress

WordPress uses dashicons as its primary icon font. However, many themes do not require dashicons on the frontend and hence it’s only used in the admin frontend, i.e. when a user is logged in.

I made a simple plugin to make WordPress load the dashicons CSS only if the user is logged in. For non-logged-in users, the dashicons CSS is removed from the frontend (hence improving the loading speed of your website).

<?php 
/*
Plugin Name: TechOverflow remove dashicons
*/

/**
 * Remove dashicons CSS from the page, only load if user is logged in
 */
function dashicons_admin_only() {
    if(!is_user_logged_in()) {
        global $wp_styles;
        wp_dequeue_style('dashicons');
        // wp_deregister_style('dashicons') causes internal PHP errors in WordPress !
        $wp_styles->registered['dashicons']->src = '';
    }
}
    
add_action( 'wp_print_styles', 'dashicons_admin_only' );

Just create an new folder called techoverflow-no-dashicons inside your wp-content/plugins directory, and save the source code listed above as wp-content/plugins/techoverflow-no-dashicons/functions.php inside. After that, you can modify the code to fit your specific needs.

Note that using this plugin might make your site appear differently if you are logged in. Hence it’s important to check your site when not logged in, e.g. using incognito mode.

Posted by Uli Köhler in PHP, Wordpress

WordPress: How to move script to footer if plugin doesn’t support it

Many WordPress plugins provide you with an option to configure whether scripts are loaded in the header or the footer of the page.

If you want to move a script to the footer for performance reasons, and the plugin doesn’t support it, one option is to just edit the wp_enqueue_script() call in the plugin’s source code and set $in_footer = true.

However, these change won’t survive plugin updates and hence are not recommended for security reasons.

A better option is to write a custom plugin that removes the <script> tag from the head and moves it to the footer. The following file is the functions.php of my custom plugin that moves the <script> tag from the cookie-law-info plugin to the bottom of the page.

<?php 
/*
Plugin Name: TechOverflow Cookie Law to footer
*/

function postpone_script($name) {
    global $wp_scripts;
    // Get attributes from original script
    $thesrc = $wp_scripts->registered[$name]->src;
    $theversion = $wp_scripts->registered[$name]->ver;
    // Remove script from the header
    wp_dequeue_script($name);
    wp_deregister_script($name);
    // Add script to the bottom
    wp_enqueue_script($name, $thesrc, false /* no deps */, $theversion, true /* bottom */);
}

/**
 * Move cookie law javascript to the bottom
 */
function postpone_cookie_law() {
    postpone_script('cookie-law-info');
}
    
add_action( 'wp_print_scripts', 'postpone_cookie_law' );

Just create an new folder called techoverflow-cookie-law-footer inside your wp-content/plugins directory, and save the source code listed above as wp-content/plugins/techoverflow-cookie-law-footer/functions.php inside. After that, you can modify the code to fit your specific needs.

Remember to check your page for issues (javascript errors, delayed rendering of some elements) that appear when you move the javascript to the footer, since some plugins may not be compatible with the script being placed in the footer. Also, you might need to call postpone_script() multiple scripts with different $name argument. Check the original plugin’s wp_enqueue_script() calls for the correct $name (first argument). In case it’s not obvious which wp_enqueue_script() call relates to a specific script, you can also check the version in the src of the original <script> tag (e.g. 1.8.2 in ...?ver=1.8.2 ) and search for that version in the plugin’s folder to find the matching wp_enqueue_script() call.

Posted by Uli Köhler in PHP, Wordpress

WordPress: Enqueue script only if user is logged in

Use this snippet in your wordpress plugin to load a script only if a user is logged in:

function enqueue_my_script() {
    if(is_user_logged_in()) {
        wp_enqueue_script( 'my-script', plugins_url('/my-script.js', __FILE__), false, '1.0', 'all');
    }
}

add_action( 'wp_enqueue_scripts', 'enqueue_my_script');

It does not matter how exactly you use wp_enqueue_script(), just wrap it in a

if(is_user_logged_in()) {
    // Call wp_enqueue_script() here !
}

 

Posted by Uli Köhler in PHP, Wordpress

Bilirubin mg/dl to μmol/l converter

Note about medical information:
This information is presented for informational purposes only and is intended for professionals. While we strive to provide accurate information, this information might be outdated, unintentionally misleading or incorrect. Consult a medical professional and/or read the primary sources cited in our article before basing any decision on this information.
TechOverflow calculators:
You can enter values with SI suffixes like 12.2m (equivalent to 0.012) or 14k (14000) or 32u (0.000032).
The results are calculated while you type and shown directly below the calculator, so there is no need to press return or click on a Calculate button. Just make sure that all inputs are green by entering valid values.

mg/dl


n[mol] = \frac{m[g]}{M_m[\frac{g}{mol}]}

Posted by Uli Köhler in Allgemein

Grams-to-mol online converter

TechOverflow calculators:
You can enter values with SI suffixes like 12.2m (equivalent to 0.012) or 14k (14000) or 32u (0.000032).
The results are calculated while you type and shown directly below the calculator, so there is no need to press return or click on a Calculate button. Just make sure that all inputs are green by entering valid values.

g

g/mol

n[mol] = \frac{m[g]}{M_m[\frac{g}{mol}]}
Posted by Uli Köhler in Calculators

Accessory cord tensile strength calculator (EN 564)

TechOverflow calculators:
You can enter values with SI suffixes like 12.2m (equivalent to 0.012) or 14k (14000) or 32u (0.000032).
The results are calculated while you type and shown directly below the calculator, so there is no need to press return or click on a Calculate button. Just make sure that all inputs are green by entering valid values.

mm

F_{\text{tensile,min}}[N] = 0.20\frac{\text{kN}}{\text{mm²}} \cdot \text{D[mm]}²
Posted by Uli Köhler in Allgemein

Sense resistor / current shunt + current sense amplifier calculator

TechOverflow calculators:
You can enter values with SI suffixes like 12.2m (equivalent to 0.012) or 14k (14000) or 32u (0.000032).
The results are calculated while you type and shown directly below the calculator, so there is no need to press return or click on a Calculate button. Just make sure that all inputs are green by entering valid values.

Don’t need a current sense amplifier? See our Sense resistor / current shunt calculator.

This calculator allows you to find out which sense resistor and current sense amplifier you need to use to obtain a specific amplifier output voltage at a specific current.

A

V

V/V

R_{\text{sense}} = \frac{U_{\text{drop}}}{I_{\text{sense}} \cdot G_{\text{current sense amplifier}}} U_{\text{sense,drop}} = I_{\text{sense}} \cdot R_{\text{sense}} P_{\text{sense}} = {I_{\text{sense}}}^2 \cdot R_{\text{sense}}
Posted by Uli Köhler in Allgemein

Sense resistor / current shunt calculator

TechOverflow calculators:
You can enter values with SI suffixes like 12.2m (equivalent to 0.012) or 14k (14000) or 32u (0.000032).
The results are calculated while you type and shown directly below the calculator, so there is no need to press return or click on a Calculate button. Just make sure that all inputs are green by entering valid values.

Too much power dissipation? You need to use a current sense amplifier ! See our Sense resistor / current shunt + current sense amplifier calculator !

This calculator allows you to find out which sense resistor you need to drop a specified voltage at a given current. Additionally it computes the power dissipated in the sense resistor.

A

V

R_{\text{sense}} = \frac{U_{\text{drop}}}{I_{\text{sense}}} P_{\text{sense}} = I_{\text{sense}}² \cdot R_{\text{sense}}
Posted by Uli Köhler in Allgemein

Relay lifetime online calculator

TechOverflow calculators:
You can enter values with SI suffixes like 12.2m (equivalent to 0.012) or 14k (14000) or 32u (0.000032).
The results are calculated while you type and shown directly below the calculator, so there is no need to press return or click on a Calculate button. Just make sure that all inputs are green by entering valid values.

Note: A cycle is typically defined as switching the relay on and switching it off again, i.e. one cycle consists of two switch actions (on + off).

In case you know how many seconds between cycles:

s

cycles

N_{\text{predicted}} = N_{\text{nominal}} \cdot \text{Seconds between cycles}

In case you know how many relay cycles per second:

Hz

cycles

N_{\text{predicted}} = \frac{N_{\text{nominal}}}{\text{Cycles per second}}
Posted by Uli Köhler in Allgemein

How to fix Raspberry Pi GPIO “No access to /dev/mem. Try running as root!”

Problem:

You want to toggle a Raspberry Pi GPIO pin using RPi.GPIO but you see an error message like

Traceback (most recent call last):
  File "gpio.py", line 8, in <module>
    GPIO.setup(pin1, GPIO.OUT)
RuntimeError: No access to /dev/mem.  Try running as root!

Solution:

Add your user to the gpio group:

sudo usermod -a -G gpio $USER

then log out and log back in (or reboot, if that doesn’t help).

The reason for this error is that /dev/gpiomem is only accessible by users belonging to the gpio group. By default, only the pi user belongs to the gpio group – hence, if you run a script as any users other than pi or root, you will see that error message.

Posted by Uli Köhler in Electronics, Raspberry Pi