user2743
user2743

Reputation: 1513

Using textscan in Matlab to handle data not properly formatted data

I'm using textscan to import data. I can get to it successfully import properly formatted data. I can't get it to properly handle data that isn't properly formatted. Below is the format of the data.

JeB2021Da 12-13 and stuff, 1, 1, 0, 1, 0, 1, 1, 1, 3, 1, 99, 0, 0, 0, 
JoB2021Ha 12-13 and stuff, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 99, 2, 1, 0, 
JoP2021Co 12-13 and stuff, not enough samples
MaA2021Be 12-13 and stuff, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 99, 1, 0, 0, 
MaA2021Ma 12-13 and stuff, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 99, 1, 0, 0,

How would I handle the data that is, not enough samples? Because currently the data structures don't line up. The data structures that are being produced are 17 x 1 and 16 x 14. I'd like to import the string as it is in the data. So not enough samples would be imported. Below is the code that I'm using.

fid = fopen('./file.txt','r');
fmt = ['%s %d %d %d %d %d %d %d %d %d %d %d %d %d %d'];
d = textscan(fid, fmt, 'CollectOutput', 1,'Delimiter',',','headerLines', 1, 'EmptyValue', 0);

I'm trying to handle it with the EmptyValue flag but it's not working. Any help is greatly appreciated.

Upvotes: 1

Views: 92

Answers (1)

ye-ti-800
ye-ti-800

Reputation: 218

I am not sure what exactly you mean by I'd like to import the string as it is in the data, or more exactly where you would like to have that string stored.

But about just reading your data as a whole you can use the 'TreatAsEmpty' argument:

d = textscan(fid, fmt, 'CollectOutput', 1,'Delimiter',',','headerLines', 1,'TreatAsEmpty','not enough samples');

Then you can modify the input further by looking for the rows in the imported data array that solely consist of zeros.

Upvotes: 2

Related Questions