Filter löschen
Filter löschen

Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

Error with importdata when looping through files in folder

1 Ansicht (letzte 30 Tage)
Nicola Fairbairn
Nicola Fairbairn am 5 Aug. 2019
Geschlossen: MATLAB Answer Bot am 20 Aug. 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 Kommentare
Srivardhan Gadila
Srivardhan Gadila am 8 Aug. 2019
Can you please mention what is the exact error you are getting and at what line?

Antworten (1)

Walter Roberson
Walter Roberson am 8 Aug. 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.

Diese Frage ist geschlossen.

Community Treasure Hunt

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

Start Hunting!

Translated by