Tuesday, 28 June 2016

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.

R Install Required Packages

When using R, if there is a script with multiple packages that might not be installed, it can take some time to go through the list and check they are all installed.

Instead of using the "require(packagename)" command, I used the following script to load packages

pkgTest <- function(x)
{
  if (!require(x,character.only = TRUE))
  {
    install.packages(x,dep=TRUE)
    if(!require(x,character.only = TRUE)) stop("Package not found")
  }
}

This script tries to load the package using the "require" command, and if this is not successful then will try to install it. It then tries the "require" command once more, and if not successful will stop the code execution.

Monday, 6 June 2016

MATLAB Error - Warning: Executing startup failed in matlabrc. In matlabrc at 212

After reinstalling MATLAB I saw the following error:

Warning: Executing startup failed in matlabrc.
This indicates a potentially serious problem in your MATLAB
setup,
which should be resolved as soon as possible.  Error detected
was:
MATLAB:FileIO:InvalidFid
Invalid file identifier.  Use fopen to generate a valid file
identifier.
> In matlabrc at 212 

This was due to a problem in my "startup.m" file which was still persistent from a previous installation, which tried to load a toolbox that was no longer installed. This was located in the following folder:

%UserProfile%\Documents\MATLAB

As I had no files there that I needed, I renamed the folder to MATLAB_old, then tried opening MATLAB again. This time it started up fine so I deleted the folder completely as it is not needed for my version (R2012a).

Saturday, 9 April 2016

Problems when programming the ESP8266-12E using Arduino software/IDE

I followed this instructables tutorial to program the ESP8266-12E. I was able to program the board the first time correctly, however trying to make changes and update the code didn't work. When I tried to update the code using the arduino IDE, I got the following messages:

warning: espcomm_sync failed
error: espcomm_open failed
error: espcomm_upload_mem failed

So I tried changing some settings with no luck. By trial and error I found the solution. On the board is a button called "Flash". This button needs to be held down when connecting the USB cable between the board and your computer. If not, it is not possible to program using the default arduino settings. I expect there is some way to enable this however I haven't yet dug deeper in to this problem, as I just wanted to get some basic functionality working.

Friday, 26 February 2016

R View more than 100 columns when exploring variable data.

I normally use R Studio when working with R. I found there is a problem when double clicking the variable name in the environment view, as the window which is displayed only shows the first 100 rows. Double clicking the variable name is the same as entering the following command:

View(data)

Instead, use the following command:

utils::View(data)

These examples assume the name "data" is the one that you want to view.

Friday, 5 February 2016

Scan WiFi networks on the raspberry pi using essid and the grep command

First of all, find the name of the wireless network adapter. Enter the command ifconfig to show each network interface along with its details. The IP address and further information is also shown, but we just need the name shown in the left most column. There will be an entry for lo, and probabaly one for eth0 if you have a connection by ethernet also. My WiFi dongle showed up as wlan0 which is typical. If you have more than one WiFi adapter, it will probabaly show up as wlan1, wlan2, etc.

Then use the following command:

sudo iwlist wlan0 scan

I have used wlan0 as the interface name, if you found something earlier in a previous step then obviously you should change wlan0 to the interface name.

I ended up getting quite a log list of networks which went off the top of the screen, with a lot of unneeded information. So it is possible to reduce the amount of information shown. To do that, we pipe the output to the grep command, which searches within the data and only displays data that matches what we are searching for. Each network contains information about the network, including the field "ESSID" which is the network ID. To only display these lines, use the following command:

sudo iwlist wlan0 scan | grep ESSID

The command is case sensitive, alternatively we can use the -i flag at the end to make it case insensitive, as follows:

sudo iwlist wlan0 scan | grep essid -i