Logo Questions Linux Laravel Mysql Ubuntu Git Menu

How do I create a string using a loop variable in MATLAB?

I have a loop like this:

for i=1:no

  %some calculations

  fid = fopen('c:\\out.txt','wt');
  %write something to the file


I want data to be written to different files like this:

  • for i=1, data is written to out1.txt
  • for i=2, data is written to out2.txt
  • for i=3, data is written to out3.txt
  • etc.

Doing 'out'+ i does not work. How can this be done?

like image 722
Lazer Avatar asked Jan 23 '23 00:01


2 Answers

Yet another option would be the function SPRINTF:

fid = fopen(sprintf('c:\\out%d.txt',i),'wt');
like image 178
gnovice Avatar answered Jan 25 '23 15:01


filename = strcat('out', int2str(i), '.txt');

like image 41
Dan Vinton Avatar answered Jan 25 '23 15:01

Dan Vinton