storing the longest string after strsplit

91 Views Asked by At

I am trying to store the longest resultant string after using the function strsplit unable to do so

eg: I have input strings such as 'R.DQDEGNFRRFPTNAVSMSADENSPFDLSNEDGAVYQRD.L'or 'L.TSNKDEEQRELLKAISNLLD'

I need store the string only between the dots (.) If there is no dot then I want the entire string. Each string may have zero, one or two dots.

part of the code which I am using:

for i=1:700
    x=regexprep(txt(i,1), '\([^\(\)]*\)','');

    y=(strsplit(char(x),'.'));

    for j=1:3

        yValues(1,j)=y{1,j};
    end
end

But the string yValues is not storing the value of y, instead showing the following error:

Assignment has more non-singleton rhs dimensions than non-singleton subscripts

What am I doing wrong and are there any suggestions on how to fix it?

1

There are 1 best solutions below

0
On BEST ANSWER

The issue is that y is a cell array and each element contains an entire string and it therefore can't be assigned to a single element in a normal array yvalues(1,j).

You need yvalues to be a cell array and then you can assign into it just fine.

yValues{j} = y{j};

Or more simply

% Outside of your loop
yValues = cell(1,3);

% Then inside of your loop
yValues(j) = y(j);

Alternately, if you just want the longest output of strsplit, you can just do something like this.

% Split the string
parts = strsplit(mystring, '.');

% Find the length of each piece and figure out which piece was the longest
[~, ind] = max(cellfun(@numel, parts));

% Grab just the longest part
longest = parts{ind};