Matlab - Log einlesen und darstellen

From XennisWiki
Jump to: navigation, search

Log einlesen (textscan)

readLog.m dient allgemein zum Einlesen von Log-Dateien.

function [ C ] = readLog( fileName, format, numHeaderLines, delimiter )
% readLog - Reads a file log
%
%   Example:   readLog('example.txt', '%s %d')
%              readLog('example.txt', '%s %d', 5, '\t')
%
%   fileName: name of log file as string
%   format: format of the content of the file
%   [numHeaderLines]: number of header lines
%   [delimiter]: Delimiter e.g. ' ' (space), '\t' (tab) or ',' (comma)
%   return: maxtrix with log data
%

    if nargin < 2
        error('Not enough input arguments.');
    elseif nargin < 3
        numHeaderLines = 0;
    elseif nargin < 4
        delimiter = ','; % ' ' or 'space'
    end
    
    if ~exist(fileName, 'file')
       error('File not exists, path: %s', fileName); 
    end
    
    fid = fopen(fileName);
    C = textscan(fid, format, 'headerLines', numHeaderLines, 'Delimiter', delimiter );
    fclose(fid);
 
end

Beispiel

Der Log ai1339429452435.log_1.txt (nur Auszug) soll mithilfe von Matlab eingelesen und grafisch dargestellt werden.

1339429652519	bce031cc7	Alex    	Active	true	SD	1339429652505	53.8726517	10.6986064	251.0	77.0	144.6	27.0	108.0	245.0	-873.0	1.0	36.0	167.0	0.0	0.0	0.0	284.0	6.0	15.0	7.7	CD	-2.140889120559982	-72.30000000000001	-61.455000000000005	 
1339429652718	bce031cc7	Alex    	Active	true	SD	1339429652710	53.8726526	10.6986033	253.0	80.0	142.5	0.0	103.0	244.0	-855.0	4.0	40.0	163.0	0.0	0.0	0.0	281.0	6.0	14.0	7.7	CD	-1.528776978417266	-45.0	-38.25	 
1339429652923	bce031cc7	Alex    	Active	true	SD	1339429652893	53.872653	10.6985993	254.6	83.0	125.7	0.0	99.0	225.0	-880.0	-15.0	43.0	165.0	0.0	0.0	0.0	274.0	6.0	14.0	7.7	CD	-0.260615748140941	-45.0	-38.25	 
1339429653118	bce031cc7	Alex    	Active	true	SD	1339429653099	53.8726537	10.6985956	256.2	84.0	148.8	0.0	107.0	211.0	-846.0	-22.0	41.0	166.0	0.0	0.0	0.0	266.0	7.0	14.0	7.7	CD	1.1163781258587526	-45.0	-38.25	 
1339429653318	bce031cc7	Alex    	Active	true	SD	1339429653300	53.8726537	10.6985956	256.2	84.0	134.1	0.0	117.0	222.0	-884.0	-43.0	43.0	165.0	0.0	0.0	0.0	257.0	7.0	12.0	7.7	CD	2.6621324539708713	-45.0	-38.25	 
1339429653518	bce031cc7	Alex    	Active	true	SD	1339429653323	53.8726547	10.6985924	257.6	86.0	142.8	0.0	125.0	208.0	-888.0	-30.0	48.0	162.0	0.0	0.0	0.0	257.0	7.0	12.0	7.7	CD	2.601369495166488	-45.0	-38.25	 
1339429653718	bce031cc7	Alex    	Active	true	SD	1339429653690	53.8726552	10.6985892	258.8	83.0	119.4	0.0	94.0	177.0	-867.0	-18.0	47.0	169.0	0.0	0.0	0.0	268.0	8.0	15.0	7.7	CD	0.781847244422823	-45.0	-38.25	 
1339429653918	bce031cc7	Alex    	Active	true	SD	1339429653883	53.8726556	10.6985862	259.8	78.0	130.9	54.0	123.0	225.0	-856.0	-5.0	35.0	170.0	0.0	0.0	0.0	278.0	5.0	18.0	7.8	CD	-1.0122202591283862	-65.44999999999999	-55.632499999999986

showRobSailLog.m

function [ output_args ] = showRobSailLog( fileName )

    format = '%u64 %s %s %s %s %s %u %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %s %f %f %f';
    
    C = readLog(fileName, format);

    time             = C{1}/1000;
    id               = C{2};
    team             = C{3};
    sActive          = C{4};
    active           = C{5};
    sSd              = C{6};
    timestamp        = C{7};
    latGps           = C{8};
    lonGps           = C{9};
    heading          = C{10};
    speed            = C{11};
    windDirection    = C{12};
    windSpeed        = C{13};
    accX             = C{14};
    accY             = C{15};
    accZ             = C{16};
    magRawX          = C{17};
    magRawY          = C{18};
    magRawZ          = C{19};
    gyroY            = C{20};
    gyroP            = C{21};
    gyrpR            = C{22};
    magY             = C{23};
    magP             = C{24};
    magR             = C{25};
    battery          = C{26};
    sCd              = C{27};
    rudder           = C{28};
    main             = C{29};
    jib              = C{30};
    
    % Mod data
    time = time - time(1);
    
    xAxis = time;
    
    prompt = {'From:','Step:','Number:'};
        dlg_title = 'Input';
        num_lines = 1;
        def = {'1','1', int2str(length(time)-1) };
    answer = inputdlg(prompt,dlg_title,num_lines,def);
    
    from=str2num(answer{1});
    step=str2num(answer{2});
    to=from+str2num(answer{3});

    i=from:step:to;

    figure(2)
    % Plot acc
    subplot(4,1,1)
    hold all
    plot(xAxis(i), accX(i), '.-')
    plot(xAxis(i), accY(i), '.-')
    plot(xAxis(i), accZ(i), '.-')
    plot(xAxis(i), speed(i))
    title('Beschleuningung')
    xlabel('Time in s')
    ylabel('Degree')
    grid on
    legend('accX', 'accY', 'accZ')   
    hold off
    
    % Plot mag
    subplot(4,1,2)
    hold all
    plot(xAxis(i), magY(i), '.-')
    plot(xAxis(i), magP(i), '.-')
    plot(xAxis(i), magR(i), '.-')
    title('Mag')
    xlabel('Time in s')
    ylabel('')
    grid on
    legend('magY', 'magP', 'magR')   
    hold off   
 
    % Plot gyro
    subplot(4,1,3)
    hold all
    plot(xAxis(i), gyroY(i), '.-')
    plot(xAxis(i), gyroP(i), '.-')
    plot(xAxis(i), gyrpR(i), '.-')
    title('Gyro')
    xlabel('Time in s')
    ylabel('')
    grid on
    legend('gyroY', 'gyroP', 'gyrpR')   
    hold off
 
    % Plot magRaw
    subplot(4,1,4)
    hold all
    plot(xAxis(i), magRawX(i), '.-')
    plot(xAxis(i), magRawY(i), '.-')
    plot(xAxis(i), magRawZ(i), '.-')
    title('MagRaw')
    xlabel('Time in s')
    ylabel('')
    grid on
    legend('magRawX', 'magRawY', 'magRawZ')   
    hold off
    
    
    % Plot wind
    figure(1)
    subplot(2,1,1)
    hold all
    plot(xAxis(i), windDirection(i), '-b')
    %plot(xAxis, windSpeed, '--m')
    plot(xAxis(i), heading(i), '-k');
    plot(xAxis(i), magY(i), '-r');
    title('Wind')
    xlabel('Time in s')
    ylabel('Degree')
    grid on
    legend('apparent wind', 'GPS Heading', 'magY')
    hold off

    % Plot servos
    subplot(2,1,2)
    hold all
    plot(xAxis(i), rudder(i), '-k')
    plot(xAxis(i), main(i), '-b')
    plot(xAxis(i), jib(i), '-r')
    hold off
    title('Servos')
    xlabel('Time in s')
    ylabel('Degree')
    grid on
    legend('rudder', 'main', 'jib')
    
end

Aufruf in Matlab

showRobSailLog('ai1339429452435.log_1.txt');

Mehrere Log-Dateien einlesen

format = '%d';
n = 10;     % Anzahl der Messungen pro Winkel
k = 100;    % Anzahl der k-letzten Werte, die verwendet werden sollen
files = [2250   4500    6750    9000]; % Dateinamen, z.b. '2250' für 2250_i.txt 

for j=1:length(files)
    disp(strcat('############ Winkel=', int2str(files(j)), ' ###########'))
    M_30 = zeros(k,n);
    for i=1:n
        fileName = strcat(int2str(files(j)), '_', int2str(i), '.txt');
        C = readLog(fileName, format);
        M_30(:,i) = C{1}(end-k+1:end);
    end
    dispLog(M_30);    
end

See also