I have to retrieve characters from a string array at particular index of array

I am creating a function to get the substrings from an array for strings but getting error Undefined function 'substring' for input arguments of type 'char'. Below is the function I wrote.Kindly, help what is wrong in that
|
function [ ] = GetSubsequence( length ,position)
seq={'ctgaataggccatctgatggaagctgaataagccatcgct','atgaataccttccatctgatggaagctgaa'};
[m ,n]=size(seq);
disp(n);
for i=1:n
sequence=seq{i};
subsequence=substring(sequence,position,length);
disp(subsequence);
end|

 Accepted Answer

Replace
subsequence = substring(sequence,position,length);
by
subsequence = sequence(position:position+length-1);
Note that "length" is a bad name for a variable also, because it is an important Matlab function.

More Answers (2)

substring does not exist as a function in my version of Matlab (Matlab R2016b) so the error message is correct in that case.
If for some reason it is mandatory to use a function named "substring" for character extraction, then you need to use the Symbolic Toolbox:
function GetSubsequence(length ,position)
seq = {'ctgaataggccatctgatggaagctgaataagccatcgct','atgaataccttccatctgatggaagctgaa'};
[m, n] = size(seq);
disp(n);
for i = 1:n
sequence = seq{i};
%strings in MuPAD must have "" around them
subsequence = feval(symengine, 'substring', ['"', sequence, '"'], position, length);
disp(subsequence);
end
I do not recommend this approach; I show it only to be pedantic about the fact that a substring function does exist.

Asked:

on 24 Feb 2017

Answered:

on 24 Feb 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!