how to concatenate multiple columns in one column of cell array?

조회 수: 16 (최근 30일)
Ebtesam Farid
Ebtesam Farid 2021년 6월 10일
댓글: Ebtesam Farid 2021년 6월 10일
Hi guys,
I have text files that have numeric data only, I wrote a loop that read all the text file in my folder and get the data and put them in one column ... but my problem is this code escape some files and hence don't give me the correct numbers of the rows
here is my code, could you tell me, where is the wrong in it?
files = dir('*.txt') ; % you are in the folder of files
N = length(files) ;
T = [];
for i = 1:N
try
filename = files(i).name ;
fid = fopen(filename, 'r');
ZWD = fscanf(fid, '%f');
T = [T; ZWD];
catch ME
disp('An error occurred while processing the files.');
disp('Execution will continue.');
continue
end
end

채택된 답변

KSSV
KSSV 2021년 6월 10일
files = dir('*.txt') ; % you are in the folder of files
N = length(files) ;
T = cell(N,1);
for i = 1:N
filename = files(i).name ;
fid = fopen(filename, 'r');
data = fscanf(fid, '%f');
T{i} = data ;
end
iwant = cell2mat(T) ;
  댓글 수: 4
Ebtesam Farid
Ebtesam Farid 2021년 6월 10일
편집: Ebtesam Farid 2021년 6월 10일
the file names are like 'eur001.txt': the first three characters are the GNSS station's name and the other three digits are Day of year (1:365)
I sometimes use (sprintf) to read them, looping on the three digits in it
Ebtesam Farid
Ebtesam Farid 2021년 6월 10일
Hi again,
I am sorry, I have a problem in the code above, with the line
iwant = cell2mat(T) ;
there are some cells contain data (don't have the same length), and therefore when converting into mat, "mat format" escape such cells and give me wrong output
for e.g.
N(lenght of the files) = 365, so I have T (cell array) with size (365 x 1), each cell contains (24 point -- hourly data) ... some cells don't have 24 points, therefore the final (iwant) variable is not 8760 point, it escapes all the different lenght cells and gave me wrong output

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Direction of Arrival Estimation에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by