Looping through every column and saving output?

I have a 96x1505 matrix. I want to run an analysis on each column then have the output for each iteration saved in a separate .txt file (creating 1505 text files). The code that needs to be looped is as follows, where x is a column of data
[c,l] = wavedec(x,6,'dmey');
cA3 = appcoef(c,l,'dmey',6);
[cD1,cD2,cD3,cD4,cD5,cD6] = detcoef(c,l,[1,2,3,4,5,6]);
D6 = wrcoef('d',c,l,'dmey',6);
D5 = wrcoef('d',c,l,'dmey',5);
D4 = wrcoef('d',c,l,'dmey',4);
D3 = wrcoef('d',c,l,'dmey',3);
D2 = wrcoef('d',c,l,'dmey',2);
D1 = wrcoef('d',c,l,'dmey',1);
So each new text file should have 6 columns in it, D1-D6.
Any help would be greatly appreciated.

 採用された回答

Stephen23
Stephen23 2015 年 5 月 22 日
編集済み: Stephen23 2015 年 5 月 22 日

0 投票

The answer really depends on many things that you have not told us: the number type (integer / float), the required file format, the desired precision, column alignment, etc. There are different functions for saving data to text files, and each of them has different options and output file formats:
For a start you can use a simple for loop, and csvwrite to save the data, something a bit like this (untested code):
ncol = size(matrix,2);
for k = 1:ncol
... code here
D = [];
D(:,6) = wrcoef('d',c,l,'dmey',6);
D(:,5) = wrcoef('d',c,l,'dmey',5);
D(:,4) = wrcoef('d',c,l,'dmey',4);
D(:,3) = wrcoef('d',c,l,'dmey',3);
D(:,2) = wrcoef('d',c,l,'dmey',2);
D(:,1) = wrcoef('d',c,l,'dmey',1);
filename = sprintf('%s_%04f.txt', 'some_name', k)
csvwrite(filename,D)
end

4 件のコメント

Robert
Robert 2015 年 5 月 26 日
編集済み: Robert 2015 年 5 月 26 日
Thank you, this is very close to what I am after, only problem being that this code seems to cycle through all columns in each loop (giving outputs of 144480x6, instead of 96x6, note that 96*1505 = 144480). I can't seem to see the problem in the code causing this, any advice?
Cheers
Walter Roberson
Walter Roberson 2015 年 5 月 26 日
Just after the "for k" you would have
[c,l] = wavedec(matrix(:,k),6,'dmey');
assuming your matrix is named "matrix"
Robert
Robert 2015 年 5 月 26 日
Thanks, fixed it right up :)
Pavel Montes de Oca
Pavel Montes de Oca 2019 年 8 月 29 日
Hello
I had the same need as above and I have solved it with your help. However, I would like to create a single matrix with all data obtained from the analysis of each column. Howcould I do this? Thanks in advance for your help.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by