Loading .txt that are in a cell array

1 view (last 30 days)
Jeroen
Jeroen on 29 Apr 2014
Answered: dpb on 29 Apr 2014
Dear Matlab users,
I'm currently facing a problem when I'm loading my data for research (used 2 sample subjects -> Proefpersoon*). When i'm loading my data into matlab it goes into a cell that looks like this:
Within these text files is data gathered from a Force plate. But I unable to get/load the data from the cell. For example, i want to filter some of the data from pp1 and pp2.... How do I acces that data trough the cell?
Hope someone can help me! :)

Answers (2)

dpb
dpb on 29 Apr 2014
You've loaded a file that seemingly contains a list of files, not the actual data.
You need to either iterate thru that list of files and open them to extract the data or, my general favorite route is to use dir to simply create the list of files of interest in a directory structure and then iterate over that to open the files...
  1 Comment
Jeroen
Jeroen on 29 Apr 2014
Edited: Jeroen on 29 Apr 2014
I understand what you're saying, but i'm not really sure what to do in Matlab to solve this!?
I've used this code to load the data:
function fileList = DataLaden(dirName)
dirData = dir(dirName);
dirIndex = [dirData.isdir];
fileList = {dirData(~dirIndex).name}';
if ~isempty(fileList)
fileList = cellfun(@(x)fullfile(dirName,x),fileList, 'UniformOutput',false);
end
subDirs = {dirData(dirIndex).name};
validIndex = ~ismember(subDirs,{'.','..'});
for iDir = find(validIndex)
nextDir = fullfile(dirName,subDirs{iDir});
fileList = [fileList; DataLaden(nextDir)];
end
end

Sign in to comment.


dpb
dpb on 29 Apr 2014
I'd more go like...
wd='D:\Desktop\BOP Metingen\Proefpersoon2'; % the directory location
d=dir(fullfile(wd,'*.txt'); % return the files there
for i=1:length(d)
% insert code to read the file here...which, exactly, function is most simple to use depends on the form of the data therein that's not been given
...
end
Rather than hardcoding that working directory if it changes would be to use the file that contains the list as input and read it from it...it's still as simple or more so to use dir to retrieve the files to process tho, imo...it just automates the directory selection.
And, of course, you can insert whatever logic needed to parse the filenames to do what needs to be done with each depending on the content of each and what want to do with them. Another "trick" since you apparently have the two sets to compare would be to differentiate in the dir call--
I'd go like...
wd='D:\Desktop\BOP Metingen\Proefpersoon2'; % the directory location
d=dir(fullfile(wd,'*.txt'); % return the files there
for i=1:length(d)
% insert code to read the file here...which, exactly, function is most simple to use depends on the form of the data therein that's not been given. I'll sample a *fopen*
fid=fopen(d(i).name,'r');
...
% when done reading, close the file..
fid=fclose(fid);
...
end
And, of course, you can insert whatever logic needed to parse the filenames to do what needs to be done with each depending on the content of each and what want to do with them. Another "trick" since you apparently have the two sets to compare would be to differentiate in the dir call--
d1=dir(fullfile(wd,'pp1*.txt'); % return the '1' set of files
d2=dir(fullfile(wd,'pp2*.txt'); % and the 2nd...
for i=1:length(d1) % both have same number
fid1=fopen(d1(i).name,'r'); % open, read each of comarative ones
fid2=fopen(d2(i).name,'r');
...
Could sort the two directories to ensure they're returned in the same order, etc., ...

Community Treasure Hunt

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

Start Hunting!