Can I store each iteration of this WHILE loop as a new variable (preferably without resorting to eval() )

7 ビュー (過去 30 日間)
I'm practicing coding by creating an interactive cross country analysis program that would allow someone to enter times from as many races as desired and run regressions on the data.
I accomplished this for a preset number of races but ran into trouble when trying to generalize the program to be able to run for n-sets of races. I had hoped to accomplish this by creating a loop that would begin entering data for a new 'average_time' variable each time 'yes' was selected from a menu.
I've heard that this could be done using the eval() function but that this method is usually discouraged, so I'd like some advice on the best method.
The pertinent part of the script I'm using is
minutes_1=input('Input race one minutes ');
seconds_1 = input('Input race one seconds ');
average_time_1=(sum(minutes_1).*60+sum(seconds_1))/7/60;
more_data=menu('Would you like to enter another race''s data?', 'yes','no');
if more_data ~=0
while more_data==1
% This part of the code is supposed to repeat the above
% process for as long as someone keeps entering 'yes' into the
% 'Would you like to enter another race''s data?' menu and creating a new average time variable for each iteration of the loop.
minutes_n=input('Input next race''s minutes ');
seconds_n = input('Input next race''s seconds ');
average_time_n=(sum(minutes_n).*60+sum(seconds_n))/7/60;
more_data=menu('Would you like to enter another race''s data?', 'yes','no');
end
else
% I would like this section to display all of the average times
% generated in minutes:seconds. I can do this for a specific average
% time using: disp(['The average time is ' num2str(mins_1) ':' num2str(secs_1)])
% Is there a way to write this as a general statement that will display average time 1 through average time n in this format?
end
Any other advice on cleaning this up would also be appreciated
  1 件のコメント
dpb
dpb 2014 年 8 月 28 日
Several choices -- simplest in idea is to use a cell array of the 1D vectors. Each race is a new cell so the number of races entered is size(cell_array).
You then process the cell array via cellfun for all or any selected subset to get the averages and/or other statistics desired.
Many other data structures could be developed that would hold the data as well, of course.

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

採用された回答

José-Luis
José-Luis 2014 年 8 月 28 日
編集済み: José-Luis 2014 年 8 月 28 日
counter = 1;
your_array = cell(some_reasonable_size,1);
while something
do_stuff;
your_array(counter) = {something};
counter = counter + 1;
end
  2 件のコメント
Matt
Matt 2014 年 8 月 28 日
Thanks. In this case would {something} just be a reference to the average_time variable, and each iteration would fill another slot of the array?
Matt
Matt 2014 年 8 月 28 日
Never mind, I figured it out. Thanks again

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by