Showing posts with label function. Show all posts
Showing posts with label function. Show all posts

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

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.