Loop for reading and extracting data from a single line of multiple text files.

I have the following code that I would like to put into a loop to read multiple text files.

txt = fileread('Test01.txt');
  temppos = strfind(txt,'Ground Temperature:');
  endpos = strfind(txt, '°C');
    for k = 1:numel(temppos)
        section{k,:} = txt(temppos(k):endpos(k));
    end
    for k = 1:numel(section)
        temp(k,:) = sscanf(section{k}, 'Ground Temperature: %f');
    end
    Results = table(temp, 'VariableNames',{'Temp'});

2 Comments

Please see the files attached.
Having issues saving multiple files. Could only save one. But the only thing changing in the data is the file name and the temp.

Sign in to comment.

 Accepted Answer

I generally do something like this —
files = dir('*.txt');
for k = 1:numel(files)
txt = fileread(files(k).name);
temppos = strfind(txt,'Ground Temperature:');
endpos = strfind(txt, '°C');
for k = 1:numel(temppos)
section{k,:} = txt(temppos(k):endpos(k));
end
for k = 1:numel(section)
temp(k,:) = sscanf(section{k}, 'Ground Temperature: %f');
end
Results{k} = table(temp, 'VariableNames',{'Temp'});
end
Results{:}
ans = table
Temp ____ 29.3
This assumes the files are already in your working directory and on your MATLAB search path.
If that is not the situation in your application, you will need something like this:
files = dir('C:\PathName\*.txt')
and then:
filename{k} = fullfile('C:\PathName\',files(k).name)
text = fileread(filename{k});
or something similar. It may be neecessary for you to experiment a bit, depending on your situation.
EDIT — Corrected typographical errors.
.

13 Comments

Thank you for the response but, the code is only saving the last run .txt file. It is not appending the data from the previous files. Can you create 3 more files and just change the file name and the temp in the file?
My pleasure!
I created ‘Results’ here as a cell array. It should save all of them, and:
Results{:}
should be a cell array of the table arrays your code creates.
You can examine each one individually as:
Results{1}
Results{2}
...
Results{n}
and so forth, for as many as you have created.
If you want to concatenate them instead, the variable names in each havee to be different, so tthis lilne:
Results{k} = table(temp, 'VariableNames',{'Temp'});
becomes:
Results{k} = table(temp, 'VariableNames',{sprintf('Temp_%d',k)});
and then you can conatenate tthem after the loop with:
AllResults = horzcat(Results{:})
To illustrate:
for k = 1:5
temp = round(rand*10,1);
Results{k} = table(temp, 'VariableNames',{sprintf('Temp_%d',k)});
end
Results{:}
ans = table
Temp_1 ______ 4.2
ans = table
Temp_2 ______ 2.7
ans = table
Temp_3 ______ 6.5
ans = table
Temp_4 ______ 9.1
ans = table
Temp_5 ______ 5.9
AllResults = horzcat(Results{:})
AllResults = 1x5 table
Temp_1 Temp_2 Temp_3 Temp_4 Temp_5 ______ ______ ______ ______ ______ 4.2 2.7 6.5 9.1 5.9
Try that. I’ll keep working with you until we get the result you want.
.
Thank you. I think there is something wrong with the counter in the for loops. They are all using the variable k. Can you share your code? As the code you supplied earlier is only outputting the value of the last text file.
These are the two additional files and their data. For some reason I’m not able to upload them as separate txt files to this question. If you could use this data and create a .txt for each and run your code. For me I’m only able to see one of the three files. It gives me an error of Index exceeds the number of array elements. Index must not exceed 1.
Thank you for your guidance.
Test
Test02
Software Version 1.2.4.1
Firmware Version er788
O432
Ground Temperature: 35.3 °C
Test
Test03
Software Version 1.2.4.1
Firmware Version er788
O432
Ground Temperature: 53.6 °C
I’ve shared everything II have. If you can post (upload) another of your .txt files, we can troubleshoot this with both of them. That should be enough to test everything.
As I illustrated, ‘Results’ should have all of them.
Matlab is not allowing me to upload more files.
The data in the Test files look like this:
Test
Test02
Software Version 1.2.4.1
Firmware Version er788
O432
Ground Temperature: 35.3 °C
If you could just copy and paste these two into two seperate text files labeled Test02.txt and Test03.txt. You can then run your code on these three files and see that it does not output Results.
Test
Test03
Software Version 1.2.4.1
Firmware Version er788
O432
Ground Temperature: 53.6 °C
k is being used as both inner and outer loop iterator. Fix that, and it should work:
files = dir('*.txt');
for n = 1:numel(files)
txt = fileread(files(n).name);
temppos = strfind(txt,'Ground Temperature:');
endpos = strfind(txt, '°C');
for k = 1:numel(temppos)
section{k,:} = txt(temppos(k):endpos(k));
end
for k = 1:numel(section)
temp(k,:) = sscanf(section{k}, 'Ground Temperature: %f');
end
Results{n} = table(temp, 'VariableNames',{'Temp'});
end
Results{:}
ans = table
Temp ____ 29.3
ans = table
Temp ____ 35.3
ans = table
Temp ____ 53.6
Oh great. Thank you for catching that. It works great!
I always use ‘k’ for a loop counter. I should have set the outer loop as ‘k1’, as I usually do in such situations.
Ok. Is there a way to get all the Results in one table instead of a cell?
Yes.
First give each table variable a different variable name:
Results{k1} = table(temp, 'VariableNames',{sprintf('Temp_%d',k1)});
(I just aded numbers here, other options are possible), then concatenate them horizontally:
AllResults = horzcat(Results{:})
That should produce a single table array with all the individual tables (actually table variables) as variables in the concatenated table. They have to have different variable names, or the concatenation step will throw an error.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2022b

Asked:

on 16 Sep 2024

Commented:

on 17 Sep 2024

Community Treasure Hunt

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

Start Hunting!