How to use textscan on a cell array without a loop?
24 次查看(过去 30 天)
显示 更早的评论
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:
filename
out
size
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',',');
end
***(e.g. store into mystruct(ii).split(nlr))
How can I use textscan, or some other method, to "vectorize" the above loop?
Thanks!
2 个评论
Walter Roberson
2019-8-17
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 ?
采纳的回答
更多回答(2 个)
Walter Roberson
2019-8-17
See my answer at https://www.mathworks.com/matlabcentral/answers/476452-splitting-one-column-into-multiple-columns# on how to process more efficiently.
3 个评论
Walter Roberson
2019-8-20
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.
Walter Roberson
2019-8-20
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', ',');
fields_cell{5}
ans =
7512.001
4412.001
7112.001
8512.001
6512.001
per isakson
2019-8-17
Am I on the right track? Run
%%
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' };
%%
chr = strjoin( mystruct.out, '\n' );
cac = textscan( chr, repmat('%s',1,15), 'Delimiter',',' );
Inspect the result
>> cac{1}
ans =
5×1 cell array
{'%ACCDA'}
{'%ACCDA'}
{'%ACCDA'}
{'%ACCDA'}
{'%ACCDA'}
>> cac{3}
ans =
5×1 cell array
{'123.99'}
{'123.99'}
{'123.99'}
{'123.99'}
{'123.99'}
>> cac{15}
ans =
5×1 cell array
{'*22'}
{'*20'}
{'*2A'}
{'*2E'}
{'*2B'}
>>
Don't you want to convert the numeric fields to double?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!