Search code examples
matlabcsvreal-timemotion-detection

Writing Real-Time data from Motion Capture System to a CSV file?


I am currently working on a project that incorporates the VICON Motion Capture System to analyze step length in subjects. The system uses infrared cameras with markers to create 3D models. The VICON system is currently set-up to use DataStreamSDK to allow for real-time data recording in MATLAB. My ultimate goal is to extract the data from one particular marker (XYZ coordinates) and export that data to a txt file so that I may edit it later). I have integrated pre-existing code as well as some of my original code to allow me to extract the coordinates for a particular marker and attempt to put those coordinates in a CSV file that adds a new coordinate with each frame. When I run the code, however, the CSV file seems to consist of only repeats of the coordinate from its most recent frame after I told the data to stop streaming. Instead, I would like the coordinates from each frame to be input onto a new line of the CSV file. I have provided the piece of the code below. If needed, I can provide the entire code, however most of it deals with enabling the streaming of the data from the VICON Nexus program. The portion that writes to the CSV file is at the bottom.

How should I edit the code so that it will update the CSV file continuously as new data is pulled, instead of simply putting 1:n repeats of the most recently pulled coordinate point? Thank you in advance.

for MarkerIndex = 9:9
  % Get the marker name
  MarkerName = MyClient.GetMarkerName( SubjectName, MarkerIndex ).MarkerName;

  % Get the marker parent
  MarkerParentName = MyClient.GetMarkerParentName( SubjectName, MarkerName ).SegmentName;

  % Get the global marker translation
  Output_GetMarkerGlobalTranslation = MyClient.GetMarkerGlobalTranslation( SubjectName, MarkerName );

  fprintf( '      Marker #%d: %s (%g, %g, %g) %s\n',                     ...
                     MarkerIndex - 1,                                    ...
                     MarkerName,                                         ...
                     Output_GetMarkerGlobalTranslation.Translation( 1 ), ...
                     Output_GetMarkerGlobalTranslation.Translation( 2 ), ...
                     Output_GetMarkerGlobalTranslation.Translation( 3 ), ...
                     AdaptBool( Output_GetMarkerGlobalTranslation.Occluded ) );



 ftemp = fopen('TestData.txt','w' );
for Output_GetFrameNumber = 1:n
    fprintf(ftemp, '%f,%f,%f\n',Output_GetMarkerGlobalTranslation.Translation( 1 ),Output_GetMarkerGlobalTranslation.Translation( 2 ),Output_GetMarkerGlobalTranslation.Translation( 3 ));
end
fclose(ftemp);
end    

Solution

  • You need to change the statement that opens the file for writing. You have:

    ftemp = fopen('TestData.txt','w' );
    

    The 'w' argument opens the file and discards the existing contents. So you are overwriting the file in every loop. If you use 'a', it will append contents to the file. See the docs here and read about the "permission" argument.

    So you can just change 'w'' to 'a' and your code should work. In addition, you could consider opening the file outside the loop:

    ftemp = fopen('TestData.txt','a');
    for MarkerIndex = 9:9
     % [insert your code for getting position data]
        for Output_GetFrameNumber = 1:n
            fprintf(ftemp, '%f,%f,%f\n',...); % [insert your print statement]
        end
    end
    close(ftemp)
    

    This will improve performance by minimizing the operations inside the loop.