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 日

1 投票

6 件のコメント

Sobel Captain
Sobel Captain 2015 年 11 月 19 日
編集済み: Sobel Captain 2015 年 11 月 19 日
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 .
Walter Roberson
Walter Roberson 2015 年 11 月 19 日
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 2015 年 11 月 19 日
So it should be in cell ?
Image Analyst
Image Analyst 2015 年 11 月 19 日
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 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 日

2 投票

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 日

1 投票

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™.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

質問済み:

2015 年 11 月 19 日

編集済み:

2019 年 6 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by