Split structured TXT files (strsplit)

Posted 2013.09.29 20:24

txt 파일의 형식

1. WiFi

#1380216393921/1::BSSID: 12:e3:c7:04:05:cb,level: -63/2::BSSID: 10:e3:c7:04:05:cb,level: -63/3::BSSID: 00:40:5a:a8:71:79,level: -62/4::BSSID: 00:01:36:5b:39:40,level: -71/5::BSSID: 00:40:5a:a8:71:7a,level: -61/6::BSSID: 00:26:66:1e:62:24,level: -72/7::BSSID: 00:1f:1f:29:56:0c,level: -74/8::BSSID: 00:40:5a:a8:56:42,level: -77/9::BSSID: 00:40:5a:a8:56:41,level: -83/10::BSSID: 00:0e:e8:f4:b1:92,level: -71/11::BSSID: 12:e3:c7:04:05:cf,level: -82/12::BSSID: 00:40:5a:a8:7c:82,level: -80/13::BSSID: 12:e3:c7:04:05:d0,level: -80/14::BSSID: 00:40:5a:a8:7c:81,level: -78/15::BSSID: 00:08:9f:d7:ae:14,level: -88/16::BSSID: 12:e3:c7:04:05:e4,level: -87/17::BSSID: 12:e3:c7:04:05:cc,level: -68/18::BSSID: 00:26:66:f2:83:80,level: -48/19::BSSID: 00:26:66:71:90:3e,level: -61/20::BSSID: 00:16:01:e6:01:2f,level: -77/21::BSSID: 00:22:be:91:35:90,level: -77/22::BSSID: 00:1c:10:b3:18:25,level: -83/23::BSSID: 00:22:57:60:e1:8d,level: -86/24::BSSID: 00:26:66:71:90:42,level: -68/25::BSSID: 00:40:5a:a8:71:7b,level: -61/26::BSSID: 00:40:5a:a8:7c:83,level: -80/27::BSSID: 10:e3:c7:04:05:e3,level: -74/28::BSSID: 10:e3:c7:04:05:cc,level: -70/29::BSSID: 10:e3:c7:04:05:d0,level: -81/30::BSSID: 00:40:5a:a8:56:43,level: -79/31::BSSID: 10:e3:c7:04:05:e4,level: -87/32::BSSID: 00:26:66:03:7c:43,level: -66/33::BSSID: 12:e3:c7:04:05:e3,level: -80/34::BSSID: 00:26:66:c4:43:40,level: -93/35::BSSID: 00:08:9f:0c:4f:f8,level: -79/36::BSSID: 10:e3:c7:04:05:cf,level: -82/37::BSSID: 00:26:66:c4:43:44,level: -85/38::BSSID: 00:26:66:45:f0:56,level: -94/39::BSSID: 00:13:10:4b:54:e6,level: -94/#1380216394011/1::BSSID: 12:e3:c7:04:05:cb,level: -63/2::BSSID: 10:e3:c7:04:05:cb,level: -63/3::BSSID: 00:40:5a:a8:71:79,level: -62/4::BSSID: 00:01:36:5b:39:40,level: -71/5::BSSID: 00:40:5a:a8:71:7a,level: -61/6::BSSID: 00:26:66:1e:62:24,level: -72/7::BSSID: 00:1f:1f:29:56:0c,level: -74/8::BSSID: 00:40:5a:a8:56:42,level: -77/9::BSSID: 00:40:5a:a8:56:41,level: -83/10::BSSID: 00:0e:e8:f4:b1:92,level: -71/11::BSSID: 12:e3:c7:04:05:cf,level: -82/12::BSSID: 00:40:5a:a8:7c:82,level: -80/13::BSSID: 12:e3:c7:04:05:d0,level: -80/14::BSSID: 00:40:5a:a8:7c:81,level: -78/15::BSSID: 00:08:9f:d7:ae:14,level: -88/16::BSSID: 12:e3:c7:04:05:e4,level: -87/17::BSSID: 12:e3:c7:04:05:cc,level: -68/18::BSSID: 00:26:66:f2:83:80,level: -48/19::BSSID: 00:26:66:71:90:3e,level: -61/20::BSSID: 00:16:01:e6:01:2f,level: -77/21::BSSID: 00:22:be:91:35:90,level: -77/22::BSSID: 00:1c:10:b3:18:25,level: -83/23::BSSID: 00:22:57:60:e1:8d,level: -86/24::BSSID: 00:26:66:71:90:42,level: -68/25::BSSID: 00:40:5a:a8:71:7b,level: -61/26::BSSID: 00:40:5a:a8:7c:83,level: -80/27::BSSID: 10:e3:c7:04:05:e3,level: -74/28::BSSID: 10:e3:c7:04:05:cc,level: -70/29::BSSID: 10:e3:c7:04:05:d0,level: -81/30::BSSID: 00:40:5a:a8:56:43,level: -79/31::BSSID: 10:e3:c7:04:05:e4,level: -87/32::BSSID: 00:26:66:03:7c:43,level: -66/33::BSSID: 12:e3:c7:04:05:e3,level: -80/34::BSSID: 00:26:66:c4:43:40,level: -93/35::BSSID: 00:08:9f:0c:4f:f8,level: -79/36::BSSID: 10:e3:c7:04:05:cf,level: -82/37::BSSID: 00:26:66:c4:43:44,level: -85/38::BSSID: 00:26:66:45:f0:56,level: -94/39::BSSID: 00:13:10:4b:54:e6,level: -94/#1380216394076/1::BSSID: 12:e3:c7:04:05:cb,level: -63/2::BSSID: 10:e3:c7:04:05:cb,level: -63/3::BSSID: 00:40:5a:a8:71:79,level: -62/4::BSSID: 00:01:36:5b:39:40,level: -71/5::BSSID: 00:40:5a:a8:71:7a,level: -61/6::BSSID: 00:26:66:1e:62:24,level: -72/7::BSSID: 00:1f:1f:29:56:0c,level: -74/8::BSSID: 00:40:5a:a8:56:42,level: -77/9::BSSID: 00:40:5a:a8:56:41,level: -83/10::BSSID: 00:0e:e8:f4:b1:92,level: -71/11::BSSID: 12:e3:c7:04:05:cf,level: -82/12::BSSID: 00:40:5a:a8:7c:82,level: -80/13::BSSID: 12:e3:c7:04:05:d0,level: -80/14::BSSID: 00:40:5a:a8:7c:81,level: -78/15::BSSID: 00:08:9f:d7:ae:14,level: -88/16::BSSID: 12:e3:c7:04:05:e4,level: -87/17::BSSID: 12:e3:c7:04:05:cc,level: -68/18::BSSID: 00:26:66:f2:83:80,level: -48/19::BSSID: 00:26:66:71:90:3e,level: -61/20::BSSID: 00:16:01:e6:01:2f,level: -77/21::BSSID: 00:22:be:91:35:90,level: -77/22::BSSID: 00:1c:10:b3:18:25,level: -83/23::BSSID: 00:22:57:60:e1:8d,level: -86/24::BSSID: 00:26:66:71:90:42,level: -68/25::BSSID: 00:40:5a:a8:71:7b,level: -61/26::BSSID: 00:40:5a:a8:7c:83,level: -80/27::BSSID: 10:e3:c7:04:05:e3,level: -74/28::BSSID: 10:e3:c7:04:05:cc,level: -70/29::BSSID: 10:e3:c7:04:05:d0,level: -81/30::BSSID: 00:40:5a:a8:56:43,level: -79/31::BSSID: 10:e3:c7:04:05:e4,level: -87/32::BSSID: 00:26:66:03:7c:43,level: -66/33::BSSID: 12:e3:c7:04:05:e3,level: -80/34::BSSID: 00:26:66:c4:43:40,level: -93/35::BSSID: 00:08:9f:0c:4f:f8,level: -79/36::BSSID: 10:e3:c7:04:05:cf,level: -82/37::BSSID: 00:26:66:c4:43:44,level: -85/38::BSSID: 00:26:66:45:f0:56,level: -94/39::BSSID: 00:13:10:4b:54:e6,level: -94/#1380216394144/1::BSSID: 12:e3:c7:04:05:cb,level: -63/2::BSSID: 10:e3:c7:04:05:cb,level: -63/3::BSSID: 00:40:5a:a8:71:79,level: -62/4::BSSID: 00:01:36:5b:39:40,level: -71/5::BSSID: 00:40:5a:a8:71:7a,level: -61/6::BSSID: 00:26:66:1e:62:24,level: -72/7::BSSID: 00:1f:1f:29:56:0c,level: -74/8::BSSID: 00:40:5a:a8:56:42,level: -77/9::BSSID: 00:40:5a:a8:56:41,level: -83/10::BSSID: 00:0e:e8:f4:b1:92,level: -71/11::BSSID: 12:e3:c7:04:05:cf,level: -82/12::BSSID: 00:40:5a:a8:7c:82,level: -80/13::BSSID: 12:e3:c7:04:05:d0,level: -80/14::BSSID: 00:40:5a:a8:7c:81,level: -78/15::BSSID: 00:08:9f:d7:ae:14,level: -88/16::BSSID: 12:e3:c7:04:05:e4,level: -87/17::BSSID: 12:e3:c7:04:05:cc,level: -68/18::BSSID: 00:26:66:f2:83:80,level: -48/19::BSSID: 00:26:66:71:90:3e,level: -61/20::BSSID: 00:16:01:e6:01:2f,level: -77/21::BSSID: 00:22:be:91:35:90,level: -77/22::BSSID: 00:1c:10:b3:18:25,level: -83/23::BSSID: 00:22:57:60:e1:8d,level: -86/24::BSSID: 00:26:66:71:90:42,level: -68/25::BSSID: 00:40:5a:a8:71:7b,level: -61/26::BSSID: 00:40:5a:a8:7c:83,level: -80/27::BSSID: 10:e3:c7:04:05:e3,level: -74/28::BSSID: 10:e3:c7:04:05:cc,level: -70/29::BSSID: 10:e3:c7:04:05:d0,level: -81/30::BSSID: 00:40:5a:a8:56:43,level: -79/31::BSSID: 10:e3:c7:04:05:e4,level: -87/32::BSSID: 00:26:66:03:7c:43,level: -66/33::BSSID: 12:e3:c7:04:05:e3,level: -80/34::BSSID: 00:26:66:c4:43:40,level: -93/35::BSSID: 00:08:9f:0c:4f:f8,level: -79/36::BSSID: 10:e3:c7:04:05:cf,level: -82/37::BSSID: 00:26:66:c4:43:44,level: -85/38::BSSID: 00:26:66:45:f0:56,level: -94/39::BSSID: 00:13:10:4b:54:e6,level: -94

... 

 

2. Position

 

1380216400/v_x:40.1235,v_y:-344.318/1380216401/v_x:40.0526,v_y:-344.321/1380216401/v_x:40.115,v_y:-

344.333/1380216402/v_x:40.0562,v_y:-344.335/1380216403/v_x:40.0855,v_y:-344.316/1380216403/v_x:40.1355,v_y:-

344.332/1380216404/v_x:40.1301,v_y:-344.33/1380216404/v_x:40.1073,v_y:-344.32/1380216405/v_x:40.1013,v_y:-

344.355/1380216406/v_x:40.0744,v_y:-344.316/1380216406/v_x:40.0611,v_y:-344.316/1380216407/v_x:40.0394,v_y:-

344.295/1380216407/v_x:40.0926,v_y:-344.315/1380216408/v_x:40.1102,v_y:-344.32/1380216409/v_x:40.0619,v_y:-

344.294/1380216410/v_x:40.0569,v_y:-344.316/1380216411/v_x:40.1055,v_y:-344.316/1380216411/v_x:40.0955,v_y:-

344.315/1380216412/v_x:40.0247,v_y:-344.296/1380216412/v_x:40.0849,v_y:-344.316/1380216413/v_x:40.0853,v_y:-

344.337/1380216413/v_x:40.0865,v_y:-344.326/1380216414/v_x:39.582,v_y:-344.727/1380216415/v_x:37.1072,v_y:-

343.882/1380216415/v_x:38.8408,v_y:-344.549/1380216416/v_x:39.4904,v_y:-345.078/

 

 

처리하는 코드

%%
clc;
clear all;
close all;

%% NAO의 위치를 읽어온다.
naoPos_fid = fopen('nao_log.txt', 'r');
nao_data = fgets(naoPos_fid);

% 저장할 위치
naoPosStruct.time = cell(1E4, 1);
naoPosStruct.x = cell(1E4, 1);
naoPosStruct.y = cell(1E4, 1);
naoPosStruct.nr_data = 0;

% '/'로 시간 / x,y 로 분리한다.
naoPos_unit = strsplit(nao_data, '/');
nr_naoPos = length(naoPos_unit);
for i = 1:nr_naoPos-1
    curr_naoPos_unit = naoPos_unit{i};
    if rem(i, 2) == 1
        % i가 홀수의 경우: 시간
        time = str2double(curr_naoPos_unit);
    else
        % i가 짝수의 경우: 위치
        % ','로 분리해서 x와 y를 분리한다.
        xy_data = strsplit(curr_naoPos_unit, ',');
        x_data = xy_data{1};
        y_data = xy_data{2};
       
        curr_x = str2double({x_data(5:end)});
        curr_y = str2double({y_data(5:end)});

%         fprintf('time: %d x: %f y: %f \n' ...
%             , time, curr_x, curr_y );
       
        naoPosStruct.nr_data = naoPosStruct.nr_data + 1;
        naoPosStruct.time{naoPosStruct.nr_data} = time;
        naoPosStruct.x{naoPosStruct.nr_data} = curr_x;
        naoPosStruct.y{naoPosStruct.nr_data} = curr_y;
    end   
end


%% WIFI data를 불러와서 시간, bssid, level로 분리시킨다.
wifi_fid = fopen('WifiLog.txt' , 'r');
wifi_data = fgets(wifi_fid);

% 저장할 위치
wifiStruct.time = cell(1E4, 1);
wifiStruct.bssid = cell(1E4, 1);
wifiStruct.level = cell(1E4, 1);
wifiStruct.nr_data = 0;

% '#' 으로 각 시간별로 분리한다.
wifi_time_unit = strsplit(wifi_data, '#');
nr_raw = length(wifi_time_unit);
for i = 2:nr_raw
   
    fprintf('[%d/%d] \n', i, nr_raw);
   
    % 각 시간에 대해서
    curr_wifi_time_unit = wifi_time_unit{i};
   
    % '/'으로 각 bssid로 분리한다.
    wifi_bssid_unit = strsplit(curr_wifi_time_unit, '/');
    nr_wifi_bssid = length(wifi_bssid_unit);
    curr_time = wifi_bssid_unit{1};
   
    for j = 2:nr_wifi_bssid-1
        % 현 시간의 각 bssid에 대해서
        curr_wifi_slash = wifi_bssid_unit{j};
       
        % ': '로 분리해서 bssid와 level을 분리한다.
        bssid_token = strsplit(curr_wifi_slash, ': ');
        temp = strsplit(bssid_token{2}, ',');
        curr_wifi_bssid_name = temp{1};
        curr_wifi_bssid_level = bssid_token{3};
       
        % 현재 시간, bssid name, bssid level
        time = str2double(curr_time)/1000;
        bssid_name = curr_wifi_bssid_name;
        level = str2double(curr_wifi_bssid_level);
%         fprintf('time: %d, bssid: %s level: %d \n' ...
%             , time, bssid_name, level);
       
        % 저장한다.
        wifiStruct.nr_data = wifiStruct.nr_data  + 1;
        wifiStruct.time{wifiStruct.nr_data} = time;
        wifiStruct.bssid{wifiStruct.nr_data} = bssid_name;
        wifiStruct.level{wifiStruct.nr_data} = level;
    end
end

%% WIFI의 bssid를 처리한다. (bssid를 unique한 수를 할당한다.)
wifiStruct.bssid_idx = cell(wifiStruct.nr_data, 1);
bssid_list.bssid_name = cell(100, 1); % 실제 bssid 의 이름
bssid_list.bssid_idx = 1:100;      % 실제 bssid 의 index
bssid_list.nr_bssid = 0;          % 저장된 bssid의 수

for i = 1:wifiStruct.nr_data
    fprintf('[%d/%d] \n', i, wifiStruct.nr_data);
    curr_time = wifiStruct.time{i};
    curr_bssid_name = wifiStruct.bssid{i};
    curr_bssid_level = wifiStruct.level{i};
   
    % 현재 bssid가 저장된 거에 있는지 확인한다.
    isNew = true;
    for j = 1:bssid_list.nr_bssid
        saved_bssid_name = bssid_list.bssid_name{j};
        if isequal(curr_bssid_name, saved_bssid_name)
            % 저장된 것이 있으면,
            isNew = false;
            % 해당 idx로 설정한다.
            wifiStruct.bssid_idx{i} = j;
            break;
        end
    end
   
    % 저장된 것에 없으면 해당 bssid를 list에 추가한다.
    if isNew
        bssid_list.nr_bssid = bssid_list.nr_bssid + 1;
        bssid_list.bssid_name{bssid_list.nr_bssid} = curr_bssid_name;
        wifiStruct.bssid_idx{i} = bssid_list.nr_bssid;
    end
end

%% bssid idx를 확인한다.
for i = 1:bssid_list.nr_bssid
    name = bssid_list.bssid_name{i};
    idx = bssid_list.bssid_idx(i);
    fprintf('bssid name: %s => idx: %d \n' ...
        , name, idx);
end

%% wifi 데이터가 어떻게 처리되었는지 확인한다.
for i = 1:wifiStruct.nr_data
    wifi_idx = wifiStruct.bssid_idx{i};
    fprintf('%d => %d \n' ...
        , i, wifi_idx);
end

%% mat으로 저장한다.
naoPosStruct    % <= 시간과 위치 정보가 들어있다.
wifiStruct      % <= 시간과 wifi 정보가 들어있다.
bssid_list      % <= wifi bssid가 idx와 연결되는 매핑이 들어있다.
save('wifi_nao_data.mat' ...
    , 'naoPosStruct', 'wifiStruct', 'bssid_list');

%%

저작자 표시 비영리 동일 조건 변경 허락
신고
« PREV : 1 : ··· : 22 : 23 : 24 : 25 : 26 : 27 : 28 : 29 : 30 : ··· : 65 : NEXT »