Thank you all again for the help yesterday, I appreciate it. But I still have some issues with my MatLab programming.
Background
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.
Lets forget what i really need to do for now, though. Lets focus on something more general. Say my string is:
abcdefghijklmnopqrstuvwxyz
and I want MatLab to output:
abcd efgh ijkl mnop qrst uvwx yz
My pitiful attempts
for k = 4:length(Vader)
Vader = horzcat(Vader(1:k), ' ', Vader(k+1:end))
k= k+5;
end
Vader
This outputs:
abcd efghijklmnopqrstuvwxyz
There are 26 ' ' (space chars) inbetween the abcd and efghijk...etc. When I try this though:
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
The output is:
abcd efgh ijkl mnop qrst uvwx yz
Totally right!
What is going on?
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)
Thank you for any and all help, I appreciate it.
EDIT
I am a doofus:
for k = 4:5:(length(Vader))
Vader = horzcat(Vader(1:k), char(13), Vader(k+1:end));
k = k+1;
end
Vader
I need the stepsize portion for this
k = 4:5:(length(Vader))
vs my old code
k = 4:(length(Vader))
However 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.
Any ideas on how to force the length(Vader) portion to update with every loop? Thank you.
EDIT 2:
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:
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
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).
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.
for k=1:4:length(Vader)
(note that I'm using thebegin:stepsize:end
range syntax, not thebegin:end
syntax that you used), but even that isn't right. Keep in mind thatlength(Vader)
in the for loop conditional will be evaluated once, before the loop starts executing. But by the time that the loop finishes,Vader
will be longer, so you're not really looping through to the end ofVader
. – Bob Gilmore