Creating a function to Import data into structures

6 visualizaciones (últimos 30 días)
Josh Tome
Josh Tome el 14 de Mzo. de 2023
Comentada: Josh Tome el 17 de Mzo. de 2023
Hello,
I am currently using an sdk to import data from a software we use in our lab. When I import the data, I bring it into matlab in structures arrays which have fields for each trial I import, and sub-fields for each marker. So the structure looks like this...
TrajX.trialname.marker_name (TrajX stands for trajectory in the x direction)
I wanted to write a function to import this data so as to down on the lines of code in the script. However I am running into an issue. When I define the output of my function to NOT include the trial name...
[TrajX] = Import_Data_Function(vicon,subject,trialname,firstframe,lastframe);
the data is imported just fine, but the first trial's data gets overwritten when I attempted to import the next trial's data. So I end up with only one field consisting of a single trial name
If I include the trial name in the output of my function...
[TrajX.(trialname)] = Import_Data_Function(vicon,subject,trialname,firstframe,lastframe);
the data is imported with a field for each trial name, but instead of a structure that consists of TrajX.trialname.marker_name, I get a structure that consists of TrajX.trialname.trialname.marker_name. Basically, there is a field for each trial name followed by another sub-field of the same trial name, then another sub-field for each marker name.
I'm not sure why this second method creates two fields/sub-fields for trial names when I only want one???
Any help on this would be greatly appreciated. I've attached the original script, function, and new script with function.
Thanks in advance.
  2 comentarios
Mathieu NOE
Mathieu NOE el 15 de Mzo. de 2023
hi
what is the way you want to have one or multiple files get organized in a data structure ?
Josh Tome
Josh Tome el 16 de Mzo. de 2023
I would like to have multiple files in a data structure. They should be organized like TrajX.trialname.marker_name, where:
TrajX = main structure
Trialname = fields below TrajX (same number of fields as there is trials)
Markername = fields below Trialname (marker names are the same for each trial)
Data = data held with the Markername field (usually a 1x? array of data)

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 15 de Mzo. de 2023
Editada: Stephen23 el 15 de Mzo. de 2023
[TrajX.(trialname),TrajY.(trialname),TrajZ.(trialname)] = Import_Data_Function(vicon,subject,firstframe,lastframe);
function [X,Y,Z] = Import_Data_Function(vicon,subject,firstframe,lastframe) % removed TRIALNAME
%% IMPORT MARKER TRAJECTORIES FROM NEXUS
% Get marker trajectory names
marker_names = vicon.GetMarkerNames(subject);
% Get marker trajectory data
% (imports data into structure arrays for TrajX,TrajY,and TrajZ. These structures have fields for each marker, and a horizontal array within that field)
X = struct();
Y = struct();
Z = struct();
for i = 1:length(marker_names)
[Xv, Yv, Zv, ~] = vicon.GetTrajectory(subject, marker_names{i});
% trims the trajectory data if a region of interest was selected
X.(marker_names{i}) = Xv(firstframe:lastframe);
Y.(marker_names{i}) = Yv(firstframe:lastframe);
Z.(marker_names{i}) = Zv(firstframe:lastframe);
end
end %End of the function
  3 comentarios
Stephen23
Stephen23 el 16 de Mzo. de 2023
Editada: Stephen23 el 16 de Mzo. de 2023
"I tried this, but unfortunately I got the following error..."
No, the error message clearly show code that does not appear in my answer.
I specifically removed TRIALNAME from the function, because it does not work. Yet the error message you showed in your comment, indicates that you are still using TRIALNAME inside the function.
I am happy to help you further with any questions or issues you have with the code from my answer.
Josh Tome
Josh Tome el 17 de Mzo. de 2023
Sorry, my apologies. I'm not sure what I did the other day when testing this code, but it was clearly incorrect. I thought I had used your code, with TRIALNAME removed, and still had issues. The error message must have been from me trying to further troublshoot by adding it back in.
In any event, I tried your code again today and it works great!
Thanks for the help.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Structures en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by