How to change my code to read a second datafile?

I've got a datafile called 'data_stair_rise'. This datafile contains measurements ( being c3dfiles ) for 5 subjects. I've created a code to read these c3dfiles. I also got a second datafile called 'data_sts'. Is it possible to not simple 'copy-paste' my code and switch 'data_stair_rise' by 'data_sts', but to load data_sts by adjusting my code?
This is my code:
aantal_proefpersonen = length(dir('data_stair_rise'))-2;
for welke_pp=1:aantal_proefpersonen %for 5 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 all c3dfiles for each subject
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 to store the data
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

6 Comments

Is the second file the same format as the first, I presume? If so, I'd recast the code slightly and turn it into a function that accepts the file name as an argument instead of hard-coding it. This could come from use of uigetfile, for example, to allow user interaction or from some other source.
For my homework I have to limit as much as possible 'input from the user', so uigetfile isn't recommend in my case... So I have to use a function for it. But how do I do this?
Well, how are you to know what the file(s) are, then, if not from the user?
If there's another file that is known that has these other files in it, then read that. But whatever it is, you have to have some way of knowing...
I want to create a function to read the datafiles 'data_stair_rise' and 'data_sts'. How do I do this?
Same as I said originally except hardcode those filenames into the top-level routine and then call the generic one with those names in a loop.
Or, use the dir solution and get
d=dir('data_*.*');
and loop thru it. If there are ones that aren't wanted, skip 'em or revise the naming scheme to have a better pattern-matching facility.
There are as many ways as people to suggest them...
So if this is my code, what should I do?
function read_data_stair_rise
%-2 vanwege twee lege plaatsen bij gebruik van dir()
aantal_proefpersonen = length(dir('data_stair_rise'))-2;
%eerste for-loop voor het totaal aantal proefpersonen (5)
for welke_pp=1:aantal_proefpersonen
myFolder =sprintf('data_stair_rise/pp%c',num2str(welke_pp));
%bepalen van het filepatroon om het later te kunnen inlezen
filePattern = fullfile(myFolder,'*.c3d');
c3dFiles = dir(filePattern);
%tweede for-loop voor het totaal aantal c3dFiles voor elke proefpersoon
for i_testen=1:length(c3dFiles);
%baseFileName gehaald uit 'dir(filePattern)'
baseFileName = c3dFiles(i_testen).name;
%volledige filename 'bouwen' met fullfile() commando
fullFileName = fullfile(myFolder, baseFileName);
%gegeven functie 'read_c3d_file()' voor het inlezen van de data
[VideoSignals,VideoSignals_headers,AnalogSignals,AnalogSignals_headers,AnalogFrameRate,VideoFrameRate] = read_c3d_file(fullFileName);
%het aanmaken van een 5x5 struct voor het bestand data_stair_rise
data_stair_rise(welke_pp, i_testen).VideoSignals = VideoSignals;
data_stair_rise(welke_pp, i_testen).VideoSignals_headers = VideoSignals_headers;
data_stair_rise(welke_pp, i_testen).AnalogSignals = AnalogSignals;
data_stair_rise(welke_pp, i_testen).AnalogSignals_headers = AnalogSignals_headers;
data_stair_rise(welke_pp, i_testen).AnalogFrameRate = AnalogFrameRate;
data_stair_rise(welke_pp, i_testen).VideoFrameRate = VideoFrameRate;
end
end
end

Answers (0)

This question is closed.

Tags

Asked:

Sam
on 26 Dec 2014

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!