How to create this function?

1 次查看(过去 30 天)
Hello, I've read how to build a function. But I'm still struggling with this... I've created a code to read c3d-files into Matlab. The output is a 5x5 struct (the 5 rows represent the subjects, and the 5 columns represent the measurements). This is my code I want to create a function for:
aantal_proefpersonen = length(dir('data_stair_rise'))-2;
for welke_pp=1:aantal_proefpersonen %for the subjects
myFolder =sprintf('data_stair_rise/pp%c',num2str(welke_pp));
filePattern = fullfile(myFolder,'*.c3d');
c3dFiles = dir(filePattern);
for i_files=1:length(c3dFiles); %for each c3dfile
baseFileName = c3dFiles(i_files).name;
fullFileName = fullfile(myFolder, baseFileName);
[VideoSignals,VideoSignals_headers,AnalogSignals,AnalogSignals_headers,AnalogFrameRate,VideoFrameRate] = read_c3d_file(fullFileName); %function to read the c3dfiles
data_stair_rise(welke_pp, i_files).VideoSignals = VideoSignals; %created a struct
data_stair_rise(welke_pp, i_files).VideoSignals_headers = VideoSignals_headers;
data_stair_rise(welke_pp, i_files).AnalogSignals = AnalogSignals;
data_stair_rise(welke_pp, i_files).AnalogSignals_headers = AnalogSignals_headers;
data_stair_rise(welke_pp, i_files).AnalogFrameRate = AnalogFrameRate;
data_stair_rise(welke_pp, i_files).VideoFrameRate = VideoFrameRate;
end
end

采纳的回答

Geoff Hayes
Geoff Hayes 2014-12-26
Sam - just go into the MATLAB editor and create a new file whose first line is
function readc3dFiles
and copy and paste your above code as the body to your function. Then save this file (the default name should be readc3dFiles.m) and call it from the command line by typing
readc3dFiles
in the command window. If you need to pass any parameters to your function or return any output parameters, you can adjust your function signature.
  2 个评论
Sam
Sam 2014-12-26
I also got a datafile called 'data_sts' but it almost the same as 'data_stair_rise'. Is it possible to adjust my code so it reads both datafiles, instead of copy-pasting the whole code and make another function that reads data_sts?
Geoff Hayes
Geoff Hayes 2014-12-27
Sam - just pass the folder name as an input parameter to your function so that your code will just use the one function to read the data from both folders. Or, pass a cell array of the two folder names and then have your code loop over the elements in your array.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by