Showing posts with label name. Show all posts
Showing posts with label name. 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





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.