Octave vectorize strsplit return value into separate variables

lvalue, octave, strsplit, vectorization

Solution

This worked.

 [friendsName favoriteColor favoriteFood] = strsplit(lineInFile, " "){1,:}

Problem

I have a file with a list of records which I parse one line at a time. Each record is newline delimited and each value is space delimited. This is just a simplified example, but it has a similar structure to the real data. ``` Bob blue pizza Sally red sushi ``` The first value is a name, then their favorite color, then their favorite food. Let's say this is in a processing loop and I want to set variables up for each value. For the first line, my values should look like this. ``` friendsName = "Bob"; favoriteColor = "blue"; favoriteFood = "pizza"; ``` I read in the line and start out with ``` lineInFile = "Bob blue pizza"; ``` strsplit seems like a good idea, but it outputs a cell array instead of a matrix of strings and I end up with ``` strsplit(lineInFile, " ") = { [1,1] = Bob [1,2] = blue [1,3] = pizza } ``` I want something like ``` {friendsName,favoriteColor,favoriteFood} = strsplit(lineInFile, " "); ``` This gives me `error: invalid lvalue function called in expression` Arrays can be used as lvalues, so I tried ``` cell2mat(strsplit(lineInFile, " ")) ans = Bobbluepizza ``` That's not what I want.

Original source