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.

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).