Dimensions of arrays being concatenated are not consistent.

2 ビュー (過去 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 件のコメント
Arnab Paul
Arnab Paul 2022 年 11 月 4 日
Thanks again
Mathieu NOE
Mathieu NOE 2022 年 11 月 7 日
as lways, my pleasure !

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by