MATLAB: Are there any examples that demonstrate how to read a comma-delimited ASCII file that contains missing data

asciicommadelimitedfilei/oMATLABread

I have a file called 'myfile.dat' which contains the following data:
date,time,current,voltage,power
12/01/60,3:56,1,2,3
12/01/61,3:57,2,4,6
12/01/67,3:58,3,,9
12/01/63,3:59,4,8
12/01/64,4:00,5,10,12
I am interested in reading the current, voltage, and power values from the file. As you can see, some data is missing so I would like to fill these spots with a zero. I would like to know what would be the best command to read such data in MATLAB.

Best Answer

The TEXTSCAN command can be used to read formatted data into MATLAB. TEXTSCAN reads data from an open text file identified by a file identifier into a cell array. You can use the command as follows:
fid = fopen('myfile.dat')
C = textscan(fid,'%s %s %d %d %d','HeaderLines',1,'Delimiter',',','EmptyValue',0)
current = C{3};
voltage = C{4};
power = C{5};
fclose(fid);
If there is missing data, TEXTSCAN will replace the missing numeric values with 0. This is a configurable property and can be changed using the EmptyValue parameter in TEXTSCAN.
For more general information on how to read data files into MATLAB, see the following link: