How to concatenate in a loop?

3 views (last 30 days)
sandy
sandy on 10 Aug 2013
A='firstname_01';'firstname_02';'firstname_03' B='lastname_01';'lastname_02';'lastname_03'
A is a set of string in .txt file 1 and B is a set of string in .txt file 2. I need to concatenate the corresponding strings in both text files and put it in the excel sheet, such that the first cell should contain firstname_01lastname_01, second cell should contain firstname_02lastname_02 and so on. Is it possible to do so in Matlab?

Answers (2)

Robert Cumming
Robert Cumming on 10 Aug 2013
yes.

Ken Atwell
Ken Atwell on 10 Aug 2013
You can import each file with code like the following:
lastnames = {};
f = fopen('B.txt');
while ~feof(f)
lastnames{end+1} = fgetl(f);
end
This will work fine for smallish files (hundreds of names), but the dynamic cell array growth will kill performance if you are working with large files. In this case, you will need to do something a bit more clever.
Once you have the two cell arrays (presumable of the same length), you can strcat them together. Then use xlswrite to emit.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!