If I have a while loop that outputs a different letter (or space) each time I run it, how can I combine all the outputted letters from each loop into a string.
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Lauren Harkness
am 19 Okt. 2017
Beantwortet: Geoff Hayes
am 19 Okt. 2017
So if loop one gave me 't' loop two outputs 'a' and loop three output 'p', I want the final string to be 'tap'
I have attached my code below. letter is the output that gives me the letter at the index I want, so I want to make a string of all the letters
fh = fopen(txt,'r');
line = fgetl(fh);
while ischar(line)
[row,rest] = strtok(line,'-')
rest = rest(2:end)
row = str2num(row)
[col,rest] = strtok(rest,'-')
rest = rest(2:end)
col = str2num(col)
[spot,~] = strtok(rest,'-')
spot = str2num(spot)
pos = dec(row,col)
pos = char(pos)
letter = pos(spot)
line = fgetl(fh);
end
0 Kommentare
Akzeptierte Antwort
Geoff Hayes
am 19 Okt. 2017
Lauren - outside of your while loop you can create an empty string variable such as
message = '';
Then, whenever you get a new letter you can add it to the message
letter = pos(spot);
message = [message letter];
So on each iteration of the loop, you will build the message to be (for example) tap.
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!