Azzera filtri
Azzera filtri

Dimensions of arrays being concatenated are not consistent.

3 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Mathieu NOE
Mathieu NOE il 3 Nov 2022
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 Commenti

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Environment and Settings in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by