Importing unequal text from text file

1 view (last 30 days)
Michi
Michi on 30 Dec 2013
Edited: Azzi Abdelmalek on 30 Dec 2013
I've got a file with the following format
if true
"abc","def","ada","bla"
"F52","L33"
.
.
.
end
with more lines following, generally all with a different number of " " entries per line.
So far I was able to import the file with textscan but it gets saved into a single column. I need each line to be a row and the strings inside the "" to be col entries. I think I can work with multiple arrays(one each line) or with an array filled with NaNs to fill the empty spaces, but have no idea how to separate the file after import

Answers (2)

Walter Roberson
Walter Roberson on 30 Dec 2013
cellfun( @(S) regexp(S, ',', 'split').', regexp(fileread('YourFile.txt'), '\n', 'split'), 'uniform', 0 )
This should give you columns across and rows down.

Azzi Abdelmalek
Azzi Abdelmalek on 30 Dec 2013
Edited: Azzi Abdelmalek on 30 Dec 2013
fid = fopen('file.txt');
res={};
while ~feof(fid)
res{end+1,1} =fgetl(fid);
end
fclose(fid);
a=regexp( res,'(?<=")[^",]+(?=")','match')
n=cellfun(@numel,a)
for k=1:numel(a)
out(k,1:n(k))=a{k};
end
out

Categories

Find more on Large Files and Big Data 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!