MATLAB Answers


xlswrite in for loop

Asked by Ali
on 20 May 2013

I wrote a genetic algorithm code about Travelling Salesman Problem. I want to run the program 100 times (each run includes 5000 iterations). In each run, the program yields the optimal route w.r.t its total distance. I want the computer to write this best solution (it is the first row o a matrix) into an excel file, each solution added in different rows in excel (i.e. the result of first run in row 1 in spreadsheet, the result of second run in second row in spreadsheet and so on). I used xlsread('experiments', L(1,:), 'exp1', 'A(counter):Q(counter)') but dinna work and gave object returned error code: 0x800A03EC. Thanks.



No products are associated with this question.

2 Answers

Answer by Image Analyst
on 20 May 2013
 Accepted answer

You should not run xlswrite() 100 times, unless you are prepared to wait a long time - a very long time. It will have to launch Excel and shutdown Excel each time you call xlswrite(). It will be so much faster to use ActiveX, and xlswrite1() from the File Exchange. I've posted ActiveX demos here before. Let me know if you want me to post it again here.


on 20 May 2013

tried what you write but there is an error in xlswrite1 function while running in my code. I will work on that soon. For now I solve the problem using workspace, I copy the related matrix from workspace. by the way you are smarter I think:) take care

Let me know what the error is. If you're going to be using Excel with MATLAB, then using ActiveX is something you should learn at some point. The basics aren't hard but sometimes if you want to do something advanced it takes a while to find the name of the right method to do what you want to do.

on 25 May 2013

hi image analyst, The error is Error using==>xlswrite1 at 142 Error:Object returned error code 0x800A03EC

Error in ==> xlswrite1 at 142 error('MATLAB:xlswrite:SelectDatarange',lasterr);

Error in ==> ullyesses16ali at 244 xlswrite1('experiments',minc,'exp1_TS_PMX_SWAP',sprintf('B%d', counter+1));

Answer by Iain
on 20 May 2013
Edited by Iain
on 20 May 2013

Define the top-left corner of where you want to write data and ensure it is a string.

xlswrite('filename.xls',L(1,:),'sheet name here',['A' num2str(counter)]);


on 20 May 2013

uuuuups got it friend:) xlswrite('experiments',L(1,:),'exp1',sprintf('A%d',counter)); it works I think:) thanks a lot anyway lian.

on 20 May 2013

If you really did put a colon between 'A' and num2str(counter) it would do that.

['A' num2str(5)] concatenates 'A' with '5' to get 'A5'

on 20 May 2013

thanks a lot again friend:)

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!