Combine writematrix with a text header and safe as file
56 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
Mathieu NOE
2021 年 1 月 4 日
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 件のコメント
Mathieu NOE
2021 年 1 月 4 日
you're welcome !!
pretty happy to start the new year on a good tempo ! at least that worked today !!
その他の回答 (1 件)
Walter Roberson
2021 年 1 月 4 日
編集済み: Walter Roberson
2022 年 8 月 23 日
'writemode', 'append'
2 件のコメント
dpb
2021 年 1 月 4 日
Interesting new addition, Walter.
Which release introduced it? R2019b lacks it which is what have here...
参考
カテゴリ
Help Center および File Exchange で Text Files についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!