extracting data from multiple txt files

Hello, I am writing a code to extract some data from multiple txt files from specific location and I am stuck at one step:
%loading all files in the folder
filedir= 'X:\GD\EA\ASP\Interest groups\TEST\targets';
files = dir(fullfile(filedir, '*.log'));
nfiles=length(files); %number of files in the specified folder
%HTLl=cell(nfiles, 8);
%HTLr=cell(nfiles, 8);
%TARGl=cell(nfiles,20);
%TARGr=cell(nfiles,20);
for i=1: nfiles
%F=files(i)
fid(i)=fopen(fullfile(filedir, files(i).name),'rt');
%fid=fopen(fullfile(filedir, files(i).name),'rt');
s{i}=textscan(fid(i), '%s', 'delimiter','\n');
for k= 1: nfiles
HTL(k)=s{1,(k)}{1,1}{8,1}
end
fclose(fid(i));
end
my s has the correct dimension but every s has three layers. When I try to extract HTL (last step) it gives me the error:
Unable to perform assignment because the indices on the left side are not compatible
with the size of the right side.
Error in loadTargets (line 36)
HTL(k)=s{1,(k)}{1,1}{8,1}
not sure how to fix this. I would appreciate some help!
K

2 Comments

Could you please upload one or couple of your txt files? You should put debug point here and look at if s is a 1X(nfiles) cell array.
HTL(k)=s{1,(k)}{1,1}{8,1}
And then you should initialize HTL before for loop as empty like below:
HTL = [];
Hi Luna, sorry I can't really upload the file as it contains customer's data. I defined HTL and broke it there but I am getting the same error message. My k is also 1 cell dimension what I guess makes sense because the loop doesn't work...

Sign in to comment.

Answers (1)

Guillaume
Guillaume on 7 Oct 2018
Edited: Guillaume on 7 Oct 2018
One obvious issue with your code is that it makes no sense to have the k loop inside the i loop. I suspect the k loop is meant to run after the i loop has terminated.
Note that s is going to be a vector, so you should be using 1d indexing not 2d. And the extra brackets around k are just clutter, so it should be s{k} not s{1, (k)}. The content of s{k} is going to be a scalar cell, so 2d indexing is once again misleading, s{k}{1} is simpler. And finally the content of s{k}{1} is going to be a vector again, so it should be s{k}{1}{8}. Note that s{k}{1}{8} is going to be a character vector, so assigning that to one element of a matrix is going to be an error. You could assign that to a cell array which seemed to be the intent with your commented out predeclaration of HTL (but why has it got 8 columns?)
So, possibly, your k loop should be:
%after the end of the i loop
HTL = cell(nfiles, 1);
for k= 1: nfiles
HTL{k}=s{k}{1}{8};
end
Note that if all you're trying to do is to extract just the 8th line of each file then there are much easier ways to do that.

1 Comment

Hi Guillaume,
that's exactly what I'm trying to do. What would be the easier way to get there?
Also, the 8th line of each file contains text and numbers, e.g. 'HTL 25 25 25 25 25 25 25' I am only interested in those numbers preferably spread to separate columns. Could you advice how I can do that?

Sign in to comment.

Categories

Asked:

on 6 Oct 2018

Commented:

on 8 Oct 2018

Community Treasure Hunt

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

Start Hunting!