Reading mixed .csv in two lines in matlab

111 Views Asked by At

I 've a csv.file part of it given below

{\rtf1\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}
\viewkind4\uc1\pard\lang1033\f0\fs17 
$GPGGA, 142621.00, 4106.29338717, N, 02901.18386180, E, 5, 06, 7.2, 103.3\par
$SDDBT, 1.3, f, 0.3, M, 0.2, F*05\par
\par
$GPGGA, 142622.00, 4106.29339273, N, 02901.18387863, E, 5, 06, 7.2, 103.3\par
$SDDBT, 1.3, f, 0.3, M, 0.2, F*05\par
\par
$GPGGA, 142623.00, 4106.29339566, N, 02901.18386326, E, 5, 06, 7.2, 103.3\par
$SDDBT, 1.3, f, 0.3, M, 0.2, F*05\par
\par
.
.
.

I 've problems \par at the end of line since I can't read value 103.3 And I couldn't make the program reads 2 lines by 2 lines (skipping "\par" line) at the same time with mixed variables

this gets rits of /par but it is not formatted reading. I am unsure using "csvread or fscanf"

fid = fopen('filename','r+'); C = textscan(fid, '%s', 'Delimiter', '\n', 'CommentStyle', '\','headerlines',2); C = C{:}; fclose(fid);

1

There are 1 best solutions below

10
On BEST ANSWER

I am not sure if textscan is able to read every nth line but I would read your file line by line using fgets:

% Initialize arrays
GPGGAArray = [];
SDDBTArray = [];

% open file
fid = fopen('filename','r+');

% Skip 2 header lines
currentLine = fgets(fid);
currentLine = fgets(fid);

while ischar(currentLine)

    currentLine = fgets(fid);
    if currentLine == -1    
        break;
    end
    % line with GPGGA
    C1 = textscan(currentLine, '%s', 'Delimiter', ','); 

    currentLine = fgets(fid);
    if currentLine == -1    
        break;
    end

    GPGGAArray = [GPGGAArray; [str2double(C1{1,1}{2}), str2double(C1{1,1}{3}), str2double(C1{1,1}{5}),...
        str2double(C1{1,1}{7}), str2double(C1{1,1}{8}), str2double(C1{1,1}{9}), str2double(C1{1,1}{10}(1:end-4))]];

    % line with SDDBT
    C2 = textscan(currentLine, '%s', 'Delimiter', ','); 

    SDDBTArray = [SDDBTArray; [str2double(C2{1,1}{2}), str2double(C2{1,1}{4}), str2double(C2{1,1}{6}),...
        str2double(C2{1,1}{7}(3:end-4))]];

    % skip line with \par
    currentLine = fgets(fid);

end
% Close file
fclose(fid);

Please be aware that C1 and C2 are cell arrays with strings.