Borrar filtros
Borrar filtros

Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

Error with importdata when looping through files in folder

1 visualización (últimos 30 días)
Nicola Fairbairn
Nicola Fairbairn el 5 de Ag. de 2019
Cerrada: MATLAB Answer Bot el 20 de Ag. de 2021
Hi all,
I'm currently trying to build a script that will run through a folder full of awkward .txt files that need converting via excel to .csv, whilst stripping out some unwanted cells that contain unwanted data to give me a 512 x 16 table of values and saving these in a new folder as .csv, although I keep getting an importdata error as it's unable to open the files.
I've had a look through my code but can't find the issue exact, but I have a feeling it might be something to do with strcat and fullfile as I've had that issue before.
Any help would be appreciated. Thanks!
data_path = 'C:\Users\nicol\Desktop\RAII-H16-F11-HM2\Fat\';
% specify where to save smoothed files
new_path = strcat(data_path, 'csvcropped/');
if ~exist(new_path, 'dir')
mkdir(new_path);
end
search_string = fullfile(data_path, '*.txt');
% find all files matching search string
files = dir(search_string);
for i = 1:numel(files)
filename = files(i).name;
name = regexprep(filename, '.txt', '');
raw_data = importdata(filename);
text_data = raw_data.textdata;
cropped_data = text_data(25:536);
tmp = cellfun(@(x)regexprep(x, '(Pixel\s+\d{1,3}:\s+)', ''), cropped_data, 'UniformOutput', false);
data_cell = cellfun(@(x)regexp(x, '\d+', 'match'), tmp, 'UniformOutput', false); clear tmp;
processed_data = array2table(str2double(table2array(cell2table(data_cell))));
new_filename = [name '.csv'];
writetable(data, strcat(new_path, new_filename), 'WriteVariableNames', false);
end
  2 comentarios
Srivardhan Gadila
Srivardhan Gadila el 8 de Ag. de 2019
Can you please mention what is the exact error you are getting and at what line?

Respuestas (1)

Walter Roberson
Walter Roberson el 8 de Ag. de 2019
raw_data = importdata( fullfile(data_path, filename));
I know that you do
search_string = fullfile(data_path, '*.txt');
but remember that the name field of the output of dir() is only the basic file name without any directory part.

La pregunta está cerrada.

Community Treasure Hunt

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

Start Hunting!

Translated by