MATLAB Answers


How to use textscan on a cell array without a loop?

Asked by rtbme17 on 17 Aug 2019
Latest activity Commented on by Walter Roberson
on 20 Aug 2019
Hi everyone!
So, I have a structure with multiple fields. One of the fields I filled with strings from multiple, fairly large text files (~100,000 lines), and it is now a cell array. Here's the layout of the structure, and a sample of mystruct.out.
mystruct =
1×5 struct array with fields:
mystruct.out =
{'%ACCDA,50,123.99,W,07512.001,E,2,11,2.2,1002.2,Z,,,,*22' }
{'%ACCDA,50,123.99,W,04412.001,E,2,11,2.2,1002.2,Z,,,,*20' }
{'%ACCDA,50,123.99,W,07112.001,E,2,11,2.2,1002.2,Z,,,,*2A' }
{'%ACCDA,50,123.99,W,08512.001,E,2,11,2.2,1002.2,Z,,,,*2E' }
{'%ACCDA,50,123.99,W,06512.001,E,2,11,2.2,1002.2,Z,,,,*2B' }
I want to avoid using a loop to perform the following, but I haven't been able to figure out how to do so on a mass scale without a loop.
I want to be able to use textscan, or some other method, to store the delimited strings into a new cell array, inside of an new mystruct field (like *** below)
Note: 'ii' is an index I'm using in a separate loop, but it's irrelevant to this issue.
for nlr = 1:mystruct(ii).size
fields = textscan(mystruct(ii).out{nlr},'%s','delimiter',',');
***(e.g. store into mystruct(ii).split(nlr))
How can I use textscan, or some other method, to "vectorize" the above loop?


I am a little confused about the sizes involved. Your show a 1x5 struct array, and 5 example output lines, but imply that there are more. Is it that case that mystruct(K).out is a 1 x 1 cell array containing a character vector for any given K? Or is mystruct(K).out an N x 1 cell array containing character vectors for any given K? If mystruct(K).out is an N x 1 cell array of character vectors, then should the data for each be processed separately, or can it all be put together into one big array ?
Hi Walter, sorry for the confusion.
The size of the structure varies, but in this case it could be expanded like so:
mystruct(1) =
filename: some value
out: Nx1 array
size: some value
mystruct(2) = ... etc.
mystruct(K).out is an Nx1 cell array
I'm hoping to process each line of mystruct(K).out, from 1:N, and store it in the new structure field but without using a loop. Does that make more sense, or should I rephrase?

Sign in to comment.

3 Answers

Answer by rtbme17 on 20 Aug 2019
Edited by rtbme17 on 20 Aug 2019
 Accepted Answer

Ok, so I actually ended up figuring something out that doesn't require textscan. I used the function 'split'.
Here's a shortened sample:
mystruct.out = {
'%ACCDA,50,123.99,W,04412.001,E,2,11,2.2,1002.2,Z,,,,*20' };
s = split(mystruct(ii).out,',',1);
s =
{'%ACCDA' } {'%ACCDA' }
{'50' } {'50' }
{'123.99' } {'123.99' }
{'W' } {'W' }
{'07512.001'} {'04412.001'}
{'E' } {'E' }
{'2' } {'2' }
{'11' } {'11' }
{'2.2' } {'2.2' }
{'1002.2' } {'1002.2' }
{'Z' } {'Z' }
{0×0 char } {0×0 char }
{0×0 char } {0×0 char }
{0×0 char } {0×0 char }
{'*22' } {'*20' }
Sorry if I wasn't clear enough in what I was asking, I was able to accomplish what I needed too so thank you all for your help!


Sign in to comment.

Answer by per isakson
on 17 Aug 2019

Am I on the right track? Run
mystruct.out = {
'%ACCDA,50,123.99,W,06512.001,E,2,11,2.2,1002.2,Z,,,,*2B' };
chr = strjoin( mystruct.out, '\n' );
cac = textscan( chr, repmat('%s',1,15), 'Delimiter',',' );
Inspect the result
>> cac{1}
ans =
5×1 cell array
>> cac{3}
ans =
5×1 cell array
>> cac{15}
ans =
5×1 cell array
Don't you want to convert the numeric fields to double?

  1 Comment

This was very close to what I needed. I ended up figuring out another way that worked well for me. Thanks!

Sign in to comment.

Answer by Walter Roberson
on 17 Aug 2019


Hmm this doesnt seem to be what I'm looking for. I don't need to join strings together, they need to be separated in order to be processed in another script.
textscan can process a character vector that has embedded newline characters, treating each as a separate line. The processing is quite efficient. This permits you to parse and convert each column of your input without having to loop sscanf or textscan over the cell entries. Give it a try.
mystruct.out = [{'%ACCDA,50,123.99,W,07512.001,E,2,11,2.2,1002.2,Z,,,,*22' }
{'%ACCDA,50,123.99,W,04412.001,E,2,11,2.2,1002.2,Z,,,,*20' }
{'%ACCDA,50,123.99,W,07112.001,E,2,11,2.2,1002.2,Z,,,,*2A' }
{'%ACCDA,50,123.99,W,08512.001,E,2,11,2.2,1002.2,Z,,,,*2E' }
{'%ACCDA,50,123.99,W,06512.001,E,2,11,2.2,1002.2,Z,,,,*2B' }];
S = strjoin(mystruct.out, '\n');
fields_cell = textscan(S, '%s%f%f%s%f%s%f%f%f%f%s%s%s%s%s', 'Delimiter', ',');
ans =

Sign in to comment.