Friday, 17 February 2017

Raspberry Pi problem with default gateway being lost when rebooting.

On my raspberry pi, I was having a problem where the default gateway was being lost every time that I rebooted it. I was able to temporarily fix the problem by using the following command:

sudo route add default gw 192.168.100.1

However when power was lost or I rebooted then I would be back to having network connection problems, which is an issue when trying to automate something.

The solution that I found was to edit the /etc/dhcpcd.conf file using the following command:

sudo nano /etc/dhcpcd.conf

Then at the bottom of the file I added the following line:

static routers=192.168.100.1

Note that if you have the same problem, your IP address might need to be different. If you enter the command ifconfig, you should get an output similar to the following:



You need to look at the section for the network you are expecting to connect to. wlan0 is provided from the built in wifi hardware which comes on the newer raspberry pis. eth0 is the ethernet connection (wired). lo is the localhost which won't be the one that you need. I was connecting with eth0, so I looked under this section at the inet addr parameter, and I tool this number and changed the last digit to a 1, which is how I got my default gateway.


The file /etc/network/interfaces did not seem to make a difference to anything. Now the only command specifically for the eth0 connection is the following:

iface eth0 inet dhcp

There are a couple of other things that I had tried with no luck.

I tried writing a shell script to add the default gateway, and then tried to use crontab to run this at reboot using the @reboot command. This did not work for some reason. Running the shell script alone was fine but it was not possible for crontab to do this. I also tried adding in a 60 second pause in the crontab before the script was ran, but this also did not work.

I also tried making modifications to the /etc/network/interfaces file, which also did not help.

I tried the following commands together as a pair:

post-up route add default gw 192.168.100.1 eth0
pre-down route del default gw 192.168.100.1 eth0

This did nothing of course. Neither did adding a line with the gateway parameter.

gateway 192.168.100.1

So all in all I'm not sure exactly where the problem came from, just I now know the /etc/network/interfaces file is the wrong place to try and fix this, and instead it needs to be changed in /etc/dhcpcd.conf

Thursday, 26 January 2017

TeXStudio, Mendeley, biblatex and biber backend problems with undefined citations

I had a problem with citation in TeXstudio when I was trying to cite an entry from Mendeley, where the \cite{citationkey} command was showing an error that the month field was incorrect. I was using biblatex as this allowed me to have the citations in the format that I wanted, but I had to use the bibtex backend, as using the biber backend gave me a lot of errors.

Some googling showed that biber is more accepting of different date formats, and that bibtex wasn't able to process the dates as exported from Mendeley.

The command I had used to select the biber backend was as follows:

\usepackage[style=authoryear,backend=biber,bibencoding=ascii,date=year]{biblatex}

However this didn't work and all of my citations gave the following message:


LaTeX Warning: Citationref:[citeationkey]' on page 1 undefined on [filename] line 21.`

The problem here ended up being the version of the biber executeable installted by MiKTeX. Firstly, I updated MiKTeX by using the following link:

Start > All Programs > MiKTeX > Update (Admin)

I had to run this a few times, as the first run would only allow me to remove obsolete packages, and not install or update anything else. Once I had ran this a couple of times, I then also ran the non admin version.

Now doing Build & View in TeXstudio was giving errors about packages being missing, so I used the MiKTeX Package Manager to install the missing packages. This is located in

Start > All Programs > MiKTeX > Package Manager (Admin)

Here, I tried installing the package that was marked as missing, but there were always more packages. So instead, I sorted by the "Installed on" field, by clicking the name of the column, and then selected all of the entries that had an empty field (i.e. had not been installed). Then I right clicked and did "Install" and then waited.

Once completed, I was still having problems in TeXstudio. The warning looked like the following:

WARN - Warning: Found biblatex control file version 2.6, expected version 2.5

So I downloaded the file for the newest version of biber from Source Forge. The repository is here: https://sourceforge.net/projects/biblatex-biber/files/biblatex-biber/ and the link to the 32 bit binary for windows is here: https://sourceforge.net/projects/biblatex-biber/files/biblatex-biber/current/binaries/Windows/biber-MSWIN32.zip/download

I downloaded this and placed it in the MiKTeX binary directory which is located here:

C:\Program Files (x86)\MiKTeX 2.9\miktex\bin

(Note that I had tried just replacing the biber.exe file with the newer version, however on it's own this hadn't worked).

Also note that it is important to delete all of the .aux files from your file that you are working from. In my case I deleted all of the files except for the .tex file.

The last thing to do if you haven't already done so, is to update the TeXstudio settings for building the document. Firstly, go to the "Bibliography" menu item, and at the bottom select Type: Biblatex.

Then go into Options > Configure TeXstudio...

Select "Build", and in the "Meta Commands" panel, find the "Default Bibliograpyh Tool" entry, click the drop down arrow and set it to "txk:///biber".

Now you should be good to go!


Friday, 26 August 2016

MATLAB Error: Subscript indices must either be real positive integers or logicals.

I was attempting to run some code in MATLAB to perform filtering, and was hit by the following error:

Subscript indices must either be real positive integers or logicals.

The code that I was using was taken from a Stack Overflow Post and the relevant line of code that generated the error is as follows:

% Process the signal with the matched filter
y = filter(b,1,x);

This was resulting in the above error. The issue was that in some previous code, I had defined the variable 'filter' as the coefficients of a filter, and this was still in my workspace. Thus MATLAB was interpreting 'filter' as a variable instead of a function, and so the parameters passed to the function were actually interpreted as indicies within the variable, which must be whole numbers. So to fix the error, before calling the line with the 'filter()' function, I just have to delete the variable with the same name. This can be done using the 'clear' command. There is the possiblity to clear the entire workspace of variables, or rather just the specific offending variable.

To clear the variable that is causing the problem, I used the following command:

clear('filter')

where filter is my variable name. Alternatively just the command 'clear' on its own will clear all variables from the workspace:

clear





Wednesday, 29 June 2016

MATLAB Undefined function 'strsplit' for input arguments of type 'char'.

I encountered the following error in MATLAB:

Undefined function 'strsplit' for input arguments of type 'char'.

This is because the function strsplit() was only added in R2013a, and any earlier versions. Instead I used the textscan() function, and set the 'delimeter' parameter to the character I wanted to split the string by.

Tuesday, 28 June 2016

Problem with MATLAB textscan only reading in first cell

I was having a problem with MATLAB using the textscan() function, where it was only reading in the data for the first cell, but wasn't throwing any errors.

I was using the following command:

T = textscan(fid, format_spec, 'HeaderLines', 0);

Evaluating T seemed to show that the file was being read correctly, with the correct number of colums matching my CSV.

>> T
T =
  Columns 1 through 4
    {1x1 cell}    [11]    [0x1 double]    [0x1 double]
  Columns 5 through 7
    [0x1 double]    [0x1 double]    [0x1 double]
  Columns 8 through 10
    [0x1 double]    [0x1 double]    [0x1 double]

The problem was that I was reading a CSV file, and the default setting for textscan uses another option for the delimiter. By leaving this field unspecified, the function was not able to handle the file correctly. I changed the line to the following:

T = textscan(fid, format_spec, 'Delimiter', ',', 'HeaderLines', 0);

This now read in the entire file correctly, revealing all of the rows were read in as expected:

>> T
T =
  Columns 1 through 2
    {81231x1 cell}    [81231x1 double]
  Columns 3 through 4
    [81231x1 double]    [81231x1 double]
  Columns 5 through 6
    [81231x1 double]    [81231x1 double]
  Columns 7 through 8
    [81231x1 double]    [81231x1 double]
  Columns 9 through 10
    [81231x1 double]    [81231x1 double]

Note that the data was read into "cells", which means that it must be handled slightly differently. In this case, if I evaluate T(1), this doesn't give all of the data from column one, I just get the following:


>> T(1)
ans =
    {81231x1 cell}

Instead, I have to use the following command:

>> T{1}
ans = 
    '2016-02-05_19-09-50'    '2016-02-05_19-10-38'    '2016-02-05_19-21-43'    '2016-02-05_19-22-31'    '2016-02-05_19-23-19'    '2016-02-05_19-24-08'    '2016-02-05_19-26-11'    '2016-02-05_19-26-59'    '2016-02-05_19-27-47'    '2016-02-05_19-28-36'

Note the curly braces around the number after T. It is also possible to only return a subset of the data by specifying the indices after the curly braces, as follows:

>> T{1}(1:3)
ans = 
    '2016-02-05_19-09-50'    '2016-02-05_19-10-38'    '2016-02-05_19-21-43'

Finally, we can get the data into a format we are more used to by using the cell2mat() function.

dates=cell2mat(T{1})

This concerts the data into a matrix. In my case I have read in strings, and so the matrix is a 2d array. Now to view the first three dates, I can use the following command:

>> dates(1:3,:)
ans =
2016-02-05_19-09-502016-02-05_19-10-382016-02-05_19-21-43

And from here the data can be treated as normal.







MATLAB Error using textscan Param/value pairs must come in pairs. Also Error using textscan Badly formed format string.

The textscan() function expects certain parameters in a specific order. If these are not as expected, the following error is given:

Error using textscan
Param/value pairs must come in pairs.

From the documentation, this format should be as follows:
C = textscan(fileID,formatSpec)

or with the optional parameters set:
C = textscan(fileID, formatSpec, Name, Value);

I was getting this error as I had originally tried to define the 'formatspec' entry name - and this turns out to be unnecessary.

I ended up using the following formatSpec for the command:

format_spec = '%s%f%f%f%f%f%f%f%f%f'

(Note no commas here)

I had also tried to use a date format string as follows:

date_format='%{yyyy-mm-dd_HH-MM-SS}D'; 

However this produced the following error:

Error using textscan
Badly formed format string.

I'm sure that the date_format string is correct, so I found that I can replace this with a simple %s to read the column into a string, and handle the data manually.

Undefined function 'readtable' for input arguments of type 'char'. or type 'double'.

There are two separate errors for the readtable() function in MATLAB that can occur for different reasons. The first is the following:

Undefined function 'readtable' for input arguments of type 'char'.

This occurs when passing a filename to the 'readtable()' function, as it expects to be passed a file hander, so instead the following usage should be used:

fid = fopen(file, 'r');
T = readtable(fid, 'Delimiter', ',', 'Format', format_spec);
flcose(fid);

The other reason for getting this error is using a version of MATLAB that is too old. If you run the code with this change, with a version of MATLAB that is too old then you will get the following error:

Undefined function 'readtable' for input arguments of type 'double'.

The readtable() function was introduced to MATLAB in R2013b, so any versions earlier than this will give this error as the function is not defined.