Creating a function to Import data into structures
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Josh Tome
am 14 Mär. 2023
Kommentiert: Josh Tome
am 17 Mär. 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 Kommentare
Mathieu NOE
am 15 Mär. 2023
hi
what is the way you want to have one or multiple files get organized in a data structure ?
Akzeptierte Antwort
Stephen23
am 15 Mär. 2023
Bearbeitet: Stephen23
am 15 Mär. 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 Kommentare
Stephen23
am 16 Mär. 2023
Bearbeitet: Stephen23
am 16 Mär. 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.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Structures finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!