0

I have a simple matlab code using transition matrix and doing monte carlo simulation. I have attached the code.

What I need you to do is: Write the results of each run in excel with the changing pattern size. The code just write the last numbers as usual. I would like to see each run results for an array called "pattern". For example "pattern" will be like this (Assuming that t=3 and;

Run1--- 45 12 17 17 17
Run2--- 56 24 24
Run3--- 7  45 45 21

In the matlab code below: pattern: Visit pattern to company, say customer visited company 2 then company... , so the pattern 2-3... Pr: Number of times company i visited Seq: Number of times company sequential visits to company i [Here is the transition matrix excel]

   clear all;
   clc;
   t=10;
      for ii=1:t
      .
      %(The codes created the pattern array)
      .
          filename=('output1.xlsx');
          xlswrite(filename,pattern,sheet1,'B2:NT2');

       end

When I use the code it gives me only the last result when ii=3;

         Run3--- 7  45 45 21

It does not have to be excel, other output file will be ok too. I tried to use sprintf but it does not work.

Thank you in advance.

Dr. Turkuaz
  • 39
  • 1
  • 10

1 Answers1

1

What you can do is create a string using sprintf:

SheetRange = sprintf('B%i:NT%i',ii+1,ii+1);
xlswrite(filename,pattern,sheet1,SheetRange);

sprintf writes a string with the specified format, in this case 'B%i:NT%i', where the %i denotes an integer should go there. Since you specify %i twice, you need two integers to go into the string, hence the ii+1,ii+1.

And of course, as always: consider not using i as a variable name

Community
  • 1
  • 1
Adriaan
  • 17,741
  • 7
  • 42
  • 75
  • Hi Adriaan, Can you clarify for ii+1 part please? – Dr. Turkuaz Jan 30 '16 at 22:50
  • Undefined function or variable 'ii'. Error in monte9 (line 64) SheetRange = sprintf('B%i:NT%i',ii+1,ii+1); – Dr. Turkuaz Jan 30 '16 at 22:50
  • @Dr.Turkuaz I used `ii` as a variable as opposed to `i`. See the link I posted on why I did that. Simply change your for loop to `for ii=1:t` – Adriaan Jan 31 '16 at 07:34
  • 1
    @Dr.Turkuaz there is an "edit" link for each of your questions. If you don't have an answer to a question, feel free to change it to your updated needs rather than asking a new question instead. And if you *do* ask a new question instead: delete the old one if it doesn't have any answers. – Andras Deak -- Слава Україні Feb 02 '16 at 19:17
  • @Dr.Turkuaz thank you for the upvote, but it is not accepting. Accepting can be done by pressing the check mark below the voting arrows on the question. – Adriaan Feb 11 '16 at 06:44