How to concatenate in a loop?

조회 수: 2 (최근 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에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by