Dimensions of arrays being concatenated are not consistent.

조회 수: 3 (최근 30일)
Arnab Paul
Arnab Paul 2022년 11월 1일
댓글: Mathieu NOE 2022년 11월 7일
I want to run this code over multiple text file. I want them as a xlsx file but showing
Dimensions of arrays being concatenated are not consistent.
Error in cell2mat (line 75)
m{n} = cat(2,c{n,:});
Error in RRSfiles (line 24)
data1 = array2table( cell2mat( textscan(as_text, fmt)), 'VariableNames', variablenames);
It worked with different files earlier though.
This is my code
myFolder = 'mydrive';
filePattern = fullfile(myFolder, '*22.txt'); % Files with .txt extension. The files are numbered as 1.txt, 2.txt etc
theFiles = dir(filePattern);
for k = 1:length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
Lines = readlines(fullFileName);
%variable names are on the first line
variablenames = regexp(Lines(1), ',', 'split');
variablenames(1:31) = ["Rrs400","Rrs410","Rrs420","Rrs430","Rrs440","Rrs450","Rrs460","Rrs470","Rrs480","Rrs490","Rrs500","Rrs510",...
"Rrs520","Rrs530","Rrs540","Rrs550","Rrs560","Rrs570","Rrs580","Rrs590","Rrs600","Rrs610","Rrs620","Rrs630","Rrs640","Rrs650","Rrs660",...
"Rrs670","Rrs680","Rrs690","Rrs700"];
Lines( cellfun(@isempty, regexp(Lines, '^\d')) ) = [];
as_text = strjoin(Lines, newline);
fmt = ''; %delimiter
data1 = array2table( cell2mat( textscan(as_text, fmt)), 'VariableNames', variablenames);
writetable(data1, [baseFileName, '.xlsx'], 'sheet',1,'Range','A1')
I have attached the sample txt file. Any help will be appreciated

채택된 답변

Mathieu NOE
Mathieu NOE 2022년 11월 3일
hello
this is my suggestion
get the data simply by this command : data = str2num(Lines(2));
myFolder = 'mydrive';
filePattern = fullfile(myFolder, '*22.txt'); % Files with .txt extension. The files are numbered as 1.txt, 2.txt etc
theFiles = dir(filePattern);
for k = 1:length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
Lines = readlines(fullFileName);
%variable names are on the first line
variablenames = regexp(Lines(1), ',', 'split');
variablenames(1:31) = ["Rrs400","Rrs410","Rrs420","Rrs430","Rrs440","Rrs450","Rrs460","Rrs470","Rrs480","Rrs490","Rrs500","Rrs510",...
"Rrs520","Rrs530","Rrs540","Rrs550","Rrs560","Rrs570","Rrs580","Rrs590","Rrs600","Rrs610","Rrs620","Rrs630","Rrs640","Rrs650","Rrs660",...
"Rrs670","Rrs680","Rrs690","Rrs700"];
Lines( cellfun(@isempty, regexp(Lines, '^\d')) ) = [];
data = str2num(Lines(2)); % <= get data
data1 = array2table(data, 'VariableNames', variablenames);
writetable(data1, [baseFileName(1:length(baseFileName)-4), '.xlsx'], 'sheet',1,'Range','A1') % remove .txt extension of baseFileName before saving to xlsx file
end
  댓글 수: 4
Mathieu NOE
Mathieu NOE 2022년 11월 7일
as lways, my pleasure !

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by