Search code examples
stringmatlabcell

How to extract the first word of a vector of strings in Matlab?


I have a dataset of companies name as below, I want to extract the first word(sometimes the second), how can I do this in Matlab. I think the idea might be telling Matlab to extract any non space letters till it encounters the first space, right? But how can I realise this idea? Thanks!

OLYMPUS CORPORATION
QUALCOMM INCORPORATED
CISCO TECHNOLOGY, INC.
SAMSUNG ELECTRONICS CO., LTD
INTERDIGITAL TECHNOLOGY CORPORATION
SUN MICROSYSTEMS, INC.

Solution

  • strtok is a Matlab function that does exactly this.

    I can only guess that you've got a cell array of strings there, each line is an element of the cell array? So in your case you'd have something like this:

    S = {'OLYMPUS CORPORATION';
         'QUALCOMM INCORPORATED'}
    
    strtok(S{1})
    

    Which outputs OLYMPUS

    or if you want the first word of every line it's just

    strtok(S)
    

    outputs

    OLYMPUS
    QUALCOMM