How to create this function?

1 visualización (últimos 30 días)
Sam
Sam el 26 de Dic. de 2014
Comentada: Geoff Hayes el 27 de Dic. de 2014
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

Respuesta aceptada

Geoff Hayes
Geoff Hayes el 26 de Dic. de 2014
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 comentarios
Sam
Sam el 26 de Dic. de 2014
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 el 27 de Dic. de 2014
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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by