using the int i as output name

1 view (last 30 days)
Sobel Captain
Sobel Captain on 19 Nov 2015
Edited: Stephen23 on 19 Jun 2019
Hi,everyone.I would like to ask a question about using the int i as output name , I hope someone could help me . such as for i=1:30; I have a function I would like to put 30 different input using for loop . just like input (i)=xxx ->my program->output (i)=xxxx I would like to have 30 input(some audio files 1.wav 2.wav 3.wav ............) and 30 output , output 1 output 2 output 3......, I have tried output(int i) that's not works , also int2str(i) not working too. could anyone tell me how to do that , thanks a lot .

Accepted Answer

Walter Roberson
Walter Roberson on 19 Nov 2015
  6 Comments
Walter Roberson
Walter Roberson on 19 Nov 2015
Yes, put it in a cell.
Sobel Captain
Sobel Captain on 19 Nov 2015
Edited: Sobel Captain on 19 Nov 2015
oh....another problems now i have 2 for loop now , outer loop i=1:10 inner loop k=1:3 and if my output want to be a cell ( output to cols and rows depends on i and k , such as i=1 k=1 fill in the 1:1 of the cell i=10 , k=3 , fill in the 10:3 ) so that's should be a 3x10 cell. and I tried to use( ans{i,k}=result; ) , the cell become 10x10 and so many blank values , is there anyway to do that , thanks a lot for your help , I am new to matlab .

Sign in to comment.

More Answers (2)

Thorsten
Thorsten on 19 Nov 2015
This is a possible framework of how to process 30 files:
for i = 1:30
input_filename = sprintf('%d.wav', i)
% read input_fileame, compute output
output_filename = sprintf('output%d', i)
% write output to file
end
  2 Comments
Sobel Captain
Sobel Captain on 19 Nov 2015
thanks a lot , but my output is not files is some values , I want it to be shown at the workspace(right side of matlab)
output_filename = sprintf('ans%d', i); output_filename=myans;
and I hope there are ans 1 ans 2 ...... ans 30 at the workspace ,myans is the result of the program, but I tried that method , not works
Thorsten
Thorsten on 19 Nov 2015
Edited: Thorsten on 19 Nov 2015
You can create dynamically named variables with eval, but this is not considered good programming practice:
eval(['ans', int2str(i) '= myans;'])
Instead, if myans has a different size for different i, use
ans{i} = myans;
or, if myans has the same size for all i:
ans(i) = myans; % if myans is a single value
or
ans(i,:) = myans; % if myans is a 1xn vector
or
ans(i, :,:) = myans; % if myans is a n x matrix
and so on.

Sign in to comment.


Stephen23
Stephen23 on 19 Nov 2015
Edited: Stephen23 on 19 Jun 2019
  2 Comments
Ilham Hardy
Ilham Hardy on 19 Nov 2015
This is everywhere :D
Stephen23
Stephen23 on 19 Nov 2015
Edited: Stephen23 on 19 Nov 2015
Yep, because beginners keep dreaming it up as The Best Way To Program™.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!