MATLAB: Different Results When Using “textscan” vs “fgetl” in MAC

MATLAB

Hi, I have a MATLAB function that processes a file and spits out a [x,y] array. The files are generated with a malformed empty line header because it has "0A0D" while the rest of the file uses "0A".
I'm using "HeaderLines" option in 'textscan' to skip lines. The number of lines calculated by 'fgetl' is correct. However 'textscan' misinterprets the number of lines in the header and skips the wrong number of lines.
How do I change the code to exclude the header consistently. Both files should return 512×1 double array, however one only returns 511×1. I cannot control what is written or how its written in the .txt files. One file has "0A0A" the other file has "0A0D". "0A0D" is supposed to be counted as 2 separate lines.

Best Answer

When a file is opened in vim and Sublime Text on Mac, there is no extra header line. However, the MATLAB Editor does display an extra header line. This is probably what is throwing off the detection code. 'fgetl' and 'textscan' are interpreting the number of header lines differently.
Two workarounds exist:
1. You can use the following call for "textscan" function:
>> dataset = textscan(fid, '%f %f', 'HeaderLines', skipLines, 'CollectOutput', 1, 'EndOfLine', '\r\n');
2. Use "readmatrix" function instead:
dataset = readmatrix(filename);
x = dataset (:,1);
y = dataset (:,2);