Combine writematrix with a text header and safe as file
17 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Simon Keegan
am 4 Jan. 2021
Bearbeitet: Walter Roberson
am 23 Aug. 2022
Hi guys,
I am stuck with this problem since many hours. I want have multible *.IV0 files (with headers). I attached one example AS A *.TXT! It was npt possible to upload *.IV0 files. So please rename it when you try to solve this problem. The fact that it is a *.IV0 file changes many things. Since with this filetype it is not possible to use some matlab codes.
I try to average all files in a specifc folder excluding the header (first 27 rows). This works great so far. Afterwards I wanted to save the averaged data by using writematrix. This works too by using following code:
folder = 'C:\Users\--__aver\';
IV0Files = dir(fullfile(folder, '*.IV0')); % Use absolute path names
numfiles = length(IV0Files);
data_sum = 0;
rows = [1,2,3,4];
for ci = 1:numfiles
data_file = readmatrix(IV0Files(ci).name,"NumHeaderLines",27,"FileType","text");
data_sum = data_sum+data_file(:,rows); % summation accross the files (removed 3rd column (time))
end
% divide by numfiles to have the average (and not the sum)
average = data_sum/numfiles;
averagecell=num2cell(average);
%this is to save the data in the new IV0 file
writematrix(average, fullfile(folder,'otherdirectionSK-ITOLspray16.IV0'),"Delimiter","tab", "FileType", "text");
But now comes the heavy part. I tried to add a header to my output file. The header should be the same as in one of the files I average. So basically the first 27 rows of the attached file. I tried soo much and I can´t figure out a way to do it. Since it is a *.IV0 file things get even more complicated. I have tried things using:
opts (does not work for *.IV0)
fgets (in a for loop (same as code above), got stuck, I think because of the tab delimiter maybe...)
3 Kommentare
Akzeptierte Antwort
Mathieu NOE
am 4 Jan. 2021
hello simon
welcome back and happy new year !
I somehow recognize this code.... and now comes the upgraded version that appends the data to the headers
try this ( after adjusting the folder path) :
folder = cd;
% folder = 'C:\Users\--__aver\';
IV0Files = dir(fullfile(folder, '*.IV0')); % Use absolute path names
numfiles = length(IV0Files);
data_sum = 0;
rows = [1,2,3,4];
for ci = 1:numfiles
data_file = readmatrix(IV0Files(ci).name,"NumHeaderLines",27,"FileType","text");
data_sum = data_sum+data_file(:,rows); % summation accross the files (removed 3rd column (time))
end
% divide by numfiles to have the average (and not the sum)
average = data_sum/numfiles;
% averagecell=num2cell(average); % not needed as we use writematrix
% retrieve headers line (from first file)
lines= readlines(IV0Files(1).name);
header_lines = convertStringsToChars(lines(1:27,:));
%this is to save the data in the new IV0 file
writecell(header_lines, fullfile([folder '\out'],'otherdirectionSK-ITOLspray16.IV0'),"Delimiter","tab", "FileType", "text");
writematrix(average, fullfile([folder '\out'],'otherdirectionSK-ITOLspray16.IV0'),"Delimiter","tab", "FileType", "text","WriteMode","append");
9 Kommentare
Mathieu NOE
am 4 Jan. 2021
you're welcome !!
pretty happy to start the new year on a good tempo ! at least that worked today !!
Weitere Antworten (1)
Walter Roberson
am 4 Jan. 2021
Bearbeitet: Walter Roberson
am 23 Aug. 2022
'writemode', 'append'
2 Kommentare
dpb
am 4 Jan. 2021
Interesting new addition, Walter.
Which release introduced it? R2019b lacks it which is what have here...
Siehe auch
Kategorien
Mehr zu Text Files finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!