%% Load matlab format VICON

clear all;close all


cd /home/shared/essua/matlab
clear all;close all;clc

load 'vicon_9.mat';

%% Write VICON to disk in c++ readable way

close all;clc

%get the labels
x_index = find(~cellfun(@isempty,vicon.tags));
y_index = x_index + 1;
z_index = x_index + 2;

tags = char(vicon.tags);

cell_tags=[];
for i=1:size(tags,1)
    first_char = findstr(tags(i,:),':');
    if ~isempty(first_char)
        cell_tags = [cell_tags cellstr(lower(tags(i,first_char+1:end)))];
    end
end
tags = cell_tags;

% tags(:,1:length('Jose Rosado:')) = [];
% tags = lower(tags);
% tags = tags(x_index);
trial_length = length(vicon.data);

for i=1:length(x_index)
    eval(sprintf('%s=vicon.data(:,%d:%d)'';',char(tags{i}),x_index(i),z_index(i)))
end

%% Write all variables into different files

output_folder = '/home/jorge/Desktop/vicon_9';
precision = '%.3f';

fprintf('Saving VICON data as txt ... ')
for i=1:length(tags)
    out_file = fullfile(output_folder,[char(tags{i}) '.txt']);
    eval(sprintf('dlmwrite(''%s'',%s'',''delimiter'','' '',''precision'',''%s'')',out_file,char(tags{i}),precision))
end
fprintf('done.\n')




