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 .

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 19 Nov. 2015

1 Stimme

6 Kommentare

Sobel Captain
Sobel Captain am 19 Nov. 2015
Bearbeitet: Sobel Captain am 19 Nov. 2015
I'm sorry , I can't get the answer there , anyway I have finished the input part already . now I just need is when i=1 output1=???? i=2 output2=????? .......... and the output are not files are some values . I hope output 1-10 shown at the work space m I have tried to use the method you provide but not work , or maybe I missed .
The answer is "DO NOT DO IT".
There are a number of different things that you can do. One would be to assign to output{i}
For example,
for K = 1 : 10
thisfilename = sprintf('Selfie%04d.tif', K);
thisimage = imread(thisfilename);
grayimage = rgb2gray(thisimage);
output{K} = grayimage;
end
This would produce output{1}, output{2}, output{3} and so on, rather than trying to produce output1, output2, output3, and so on.
Sobel Captain
Sobel Captain am 19 Nov. 2015
So it should be in cell ?
Image Analyst
Image Analyst am 19 Nov. 2015
It doesn't need to be a cell array - it depends what your output is. If the output is just a single number, or an array of numbers, you can use a numerical array instead of a cell array. If the output is a string of different lengths each time, then you'll need a cell array.
Walter Roberson
Walter Roberson am 19 Nov. 2015
Yes, put it in a cell.
Sobel Captain
Sobel Captain am 19 Nov. 2015
Bearbeitet: Sobel Captain am 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 .

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Thorsten
Thorsten am 19 Nov. 2015

2 Stimmen

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 Kommentare

Sobel Captain
Sobel Captain am 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 am 19 Nov. 2015
Bearbeitet: Thorsten am 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.

Melden Sie sich an, um zu kommentieren.

Stephen23
Stephen23 am 19 Nov. 2015
Bearbeitet: Stephen23 am 19 Jun. 2019

1 Stimme

2 Kommentare

Ilham Hardy
Ilham Hardy am 19 Nov. 2015
This is everywhere :D
Stephen23
Stephen23 am 19 Nov. 2015
Bearbeitet: Stephen23 am 19 Nov. 2015
Yep, because beginners keep dreaming it up as The Best Way To Program™.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by