Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Assume the following code (Below code is just an example to clearly illustrate my problem):

Res = {};
Iterations = 5;

Results = AppendTo[Res, i^2]
, {i, 1, Iterations}];

I have 2 questions:

  1. I want to save the results in each iteration in an identical excel file (.xls file). In the end, when the iterations are finished, I will have one Excel file with all the results in it.

  2. I tried to make the code more faster and elegant using Scan[] , but I failed to do it and I couldn't do that.(It is good to mention that I am just a rookie Mathematica programmer and I don't have much experience in it!).

Could you please help me with these poroblems ?

share|improve this question
I'm not sure if it's possible to export data to a pre-existing Excel file. Personally, I'd collect the results from the loop and export them at the end. Also, Do is in no way a recursive construct. Are you sure you mean recursive? –  Oleksandr R. Apr 30 '14 at 8:23
the standard Export for excel files can only write content in one go. Of course you can completely overwrite the file for each line, which is inefficient but probably not too problematic. Other than that I see the following possibilities: write to a text file (or CSV) line by line, for these appending is simple and efficient. Depending on the data, you won't loose much compared to an excel file. If you really need excel, you could use NETLink to do what you want, if you search for "NET excel" you'll find many Q+A to start... –  Albert Retey Apr 30 '14 at 8:38
@Shellp: yes, you could write a new file per iteration, but if iterations are "quick", you'll waste time for file operations. –  ciao Apr 30 '14 at 9:46
And then there is ExcelLink ... but not for free –  belisarius Apr 30 '14 at 12:06
If one iteration takes 5 minutes and two iterations take 11 minutes then the second iteration must be taking 6 minutes. If the pattern continues (third iteration takes 7 minutes and so on) the total time should be over 24 hours. So the question is why does each iteration take longer than the last? We can't answer that without knowing what you're actually doing. –  Simon Woods Apr 30 '14 at 16:09

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.