using the int i as output name

조회 수: 1 (최근 30일)
Sobel Captain
Sobel Captain 2015년 11월 19일
편집: Stephen23 2019년 6월 19일
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 .

채택된 답변

Walter Roberson
Walter Roberson 2015년 11월 19일
  댓글 수: 6
Walter Roberson
Walter Roberson 2015년 11월 19일
Yes, put it in a cell.
Sobel Captain
Sobel Captain 2015년 11월 19일
편집: Sobel Captain 2015년 11월 19일
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 .

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

추가 답변 (2개)

Thorsten
Thorsten 2015년 11월 19일
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
Sobel Captain
Sobel Captain 2015년 11월 19일
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 2015년 11월 19일
편집: Thorsten 2015년 11월 19일
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.

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


Stephen23
Stephen23 2015년 11월 19일
편집: Stephen23 2019년 6월 19일
  댓글 수: 2
Ilham Hardy
Ilham Hardy 2015년 11월 19일
This is everywhere :D
Stephen23
Stephen23 2015년 11월 19일
편집: Stephen23 2015년 11월 19일
Yep, because beginners keep dreaming it up as The Best Way To Program™.

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by