title: MatLab: Need to add a ' \t' (tab char) every 4 chars in a string
body: <p>Thank you all again for the help yesterday, I appreciate it. But I still have some issues with my MatLab programming. </p>
  
  <p><strong>Background</strong></p>
  
  <p>I have a lengthy string (200k+ chars) that I need to parse. It's all in Hex and I need to get MatLab to plot it. I'm trying to break down the steps. I need to put a '\t' (tab char) every 4 chars in this string. </p>
  
  <p>Lets forget what i really need to do for now, though. Lets focus on something more general. Say my string is:</p>
  
  <pre><code>abcdefghijklmnopqrstuvwxyz
  </code></pre>
  
  <p>and I want MatLab to output:</p>
  
  <pre><code>abcd efgh ijkl mnop qrst uvwx yz
  </code></pre>
  
  <p><strong>My pitiful attempts</strong></p>
  
  <pre><code>for k = 4:length(Vader)
      Vader = horzcat(Vader(1:k), ' ', Vader(k+1:end))
      k= k+5;
  end
  Vader
  </code></pre>
  
  <p>This outputs:</p>
  
  <pre><code>abcd                         efghijklmnopqrstuvwxyz
  </code></pre>
  
  <p>There are 26 ' ' (space chars) inbetween the abcd and efghijk...etc. 
  When I try this though:</p>
  
  <pre><code>k= 4;
  Vader = horzcat(Vader(1:k), ' ', Vader(k+1:end))
  k= k+5;
  Vader = horzcat(Vader(1:k), ' ', Vader(k+1:end))
  k= k+5;
  Vader = horzcat(Vader(1:k), ' ', Vader(k+1:end))
  k= k+5;
  .....etc
  </code></pre>
  
  <p>The output is: </p>
  
  <pre><code>abcd efgh ijkl mnop qrst uvwx yz
  </code></pre>
  
  <p>Totally right!</p>
  
  <p><strong>What is going on?</strong></p>
  
  <p>Why is the for loop different than just writing out all my steps? They seem the same to me. I am lost. Also, I need this to do it for any string, any character I want to put in (\t, \n, 'somechars', etc) and for any length string. My eventual goal is to get it to handle hex strings of 4 chars in groups of 6 with 2 stop/start chars at the end (28 total chars)</p>
  
  <p>Thank you for any and all help, I appreciate it.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I am a doofus:</p>
  
  <pre><code>for k = 4:5:(length(Vader))
      Vader = horzcat(Vader(1:k), char(13), Vader(k+1:end));
      k = k+1; 
  end
  Vader
  </code></pre>
  
  <p>I need the stepsize portion for this</p>
  
  <pre><code>k = 4:5:(length(Vader))
  </code></pre>
  
  <p>vs my old code</p>
  
  <pre><code>k = 4:(length(Vader))
  </code></pre>
  
  <p><strong>However</strong> this will not loop through the entire thing. As a commenter below points out, the length(Vader) part is initialized at the beginning. The code will then stop before the desired output is reached and the string is now longer that it initially was. </p>
  
  <p>Any ideas on how to force the length(Vader) portion to update with every loop? Thank you.</p>
  
  <p><strong>EDIT 2:</strong></p>
  
  <p>Ok, got it! You have to pre-calculate the length your final string is going to be and have that be the limit for the 'for' loop:</p>
  
  <pre><code>jarJar = 4;         %how many in string you want to skip over
  luke = ceil(length(Vader)*((1+jarJar)/jarJar));
  for hanSolo = jarJar:(1+jarJar):luke
      Vader = horzcat(Vader(1:hanSolo), ' ', Vader(hanSolo+1:end));
      hanSolo = hanSolo+1; 
  end
  Vader
  </code></pre>
  
  <p>The ceil() part is important. You can get fractional values and you want to go past the end of the final string length, not under it (as floor() would give you).</p>
  
  <p>Thanks for the help Bob Gilman and the brain pickings. It really helps to have a rubber duck that can sometimes talk back to you.</p>