Exporting data from for loop into text file

Currently I have a for loop running and I'm trying to export the data from the for loop into a text file. I tried the following but it gives me all the data in a row:
x = (1:size(BOLD,1))./1.16E6;
y = BOLD;
xy = [x(:), y(:)];
dlmwrite('YourOutputFile.txt', xy, 'delimiter', ',');
(BOLD is the variable that I plotted which I need the numerical data for)
Is there a way to export the data in a way so that it gives me all the data sets in separate files (one file per graph produced)?
Thank you in advance!

 채택된 답변

per isakson
per isakson 2019년 4월 3일
편집: per isakson 2019년 4월 3일

0 개 추천

"gives me all the data in a row:" This script produces two columns
%%
BOLD = reshape( (1:6), [],1 );
x = (1:size(BOLD,1))./1.16E6;
y = BOLD;
xy = [x(:),y(:)];
%%
dlmwrite('d:\tmp\YourOutputFile.txt', xy, 'delimiter', ',');
%%
type('d:\tmp\YourOutputFile.txt')
outputs
8.6207e-07,1
1.7241e-06,2
2.5862e-06,3
3.4483e-06,4
4.3103e-06,5
5.1724e-06,6
"all the data sets in separate files" You need a new filename for each iteration of the loop
In response to comment:
"the data from the for loop into a text file"
To append new data to an existing file use
dlmwrite(filename,M,'-append')
See dlmwrite, (Not recommended) Write matrix to ASCII-delimited file. (If the file doesn't exist a new file is created.)
"all the data sets in separate files", "have a new filename for each iteration?"
My way
%%
for jj = 1 : 3
name = sprintf( 'YourOutputFile_%04d.txt', jj );
ffs = fullfile( 'd:\tmp', name );
dlmwrite( ffs, xy, 'delimiter',',' );
end
outputs
name =
'YourOutputFile_0001.txt'
name =
'YourOutputFile_0002.txt'
name =
'YourOutputFile_0003.txt'
and there are others

댓글 수: 2

Hima Sheth
Hima Sheth 2019년 4월 3일
Hi! I'm fairly new to MATLAB so how would I program it so that I have a new filename for each iteration?
per isakson
per isakson 2019년 4월 3일
See my answer

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

태그

질문:

2019년 4월 2일

편집:

2019년 4월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by