0

I have a cell array of strings. I need to extract say 1-to-n characters for each item. Strings are always longer than n characters. Please see:

data = { 'msft05/01/2010' ;
         'ap01/01/2013' }

% For each string, last 10 characters are removed and put it in the next column

answer = { 'msft' '05/01/2010' ;
           'ap'   '01/01/2013' }

Is there a vectorized solution possible? I have tried using cellfun but wasn't successful. Thanks.

1 Answer 1

3
data = { 'msft05/01/2010' ;
         'ap01/01/2013' };
for i = 1:length(data)
    s = data{i};
    data{i} = {s(1:end-10) s(end-9:end)};
end

Sorry, didn't notice that you need vectorized... Perhaps I can suggest only one-liner...

data = cellfun(@(s) {s(1:end-10) s(end-9:end)}, data, 'UniformOutput', false);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Users can then do vertcat(data{:}) to get the final ans.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.