textscan output into one cell
Show older comments
I'm reading in a text file that contains many sets of strings separated by line and then by tab. I need to use textscan or something similar because of variability in the txt files I'll be using. Currently, I'm using code like the below to separate each of the strings into their own cell array, and then haphazardly group them into one cell(my desired output). I'm wondering if there is a more efficient way to go about this, and if it's possible to do this without the second loop or even the second text scan. Thank you in advance for your help!
for i=1:length(data)
check=char(data{i});
row= textscan(check, '%s', 'Delimiter', ' ');
for j= 1:length(row{1})
a{i,j}=row{1}{j}
end
end
Example text format:
disorganizedstring
disorganized string string
stringtype1 stringtype2 stringtype3 stringtype4
stringtype1 stringtype2 stringtype3 stringtype4
stringtype1 stringtype2 stringtype3 stringtype4
stringtype1 stringtype2 stringtype3 stringtype4
stringtype1 stringtype2 stringtype3 stringtype4
Current output:
'stringtype1' 'stringtype2' 'stringtype3' 'stringtype4'
'stringtype1' 'stringtype2' 'stringtype3' 'stringtype4'
'stringtype1' 'stringtype2' 'stringtype3' 'stringtype4'
'stringtype1' 'stringtype2' 'stringtype3' 'stringtype4'
'stringtype1' 'stringtype2' 'stringtype3' 'stringtype4'
Accepted Answer
More Answers (0)
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!