How to concatenate in a loop?

3 次查看(过去 30 天)
sandy
sandy 2013-8-10
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?

回答(2 个)

Robert Cumming
Robert Cumming 2013-8-10
yes.

Ken Atwell
Ken Atwell 2013-8-10
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.

类别

Help CenterFile Exchange 中查找有关 Cell Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by