Textscan - remove rows with special character

1 view (last 30 days)
I'm trying to read in a dataset, though it contains some rows with missing values which needs to be removed. The missing values are represented by a '?' in the dataset.
example of 3 rows:
1056784,3,1,1,1,2,1,2,1,1,2
1057013,8,4,5,1,2,?,7,3,1,4
1059552,1,1,1,1,2,1,3,1,1,2
Right now i'm doing this:
fid = fopen('bcw.data');
adata = textscan(fid,'%f%f%f%f%f%f%f%f%f%f%f','delimiter',',');
fclose(fid);
I need to remove the middle row (along with any other that contain a '?'), can anyone help with this? Thanks

Accepted Answer

Walter Roberson
Walter Roberson on 4 Jan 2016
Did you try the TreatAsEmpty option? When used, the value that will be substituted is controlled by the EmptyValue option, default NaN.
adatacell = textscan(fid,'%f%f%f%f%f%f%f%f%f%f%f','delimiter',',', 'TreatAsEmpty', '?', 'CollectOutput', 1);
adata = adatacell{1};
adata(any(isnan(adata),2),:) = []; %delete rows that have a NaN
  1 Comment
Mathias Fynbo
Mathias Fynbo on 4 Jan 2016
Thanks, this did exactly what i wanted. I saw the TreatAsEmpty in the docu, but didn't fully understand it's usage. Thanks again.

Sign in to comment.

More Answers (0)

Categories

Find more on Characters and Strings 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!