Effacer les filtres
Effacer les filtres

Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

Error with importdata when looping through files in folder

1 vue (au cours des 30 derniers jours)
Nicola Fairbairn
Nicola Fairbairn le 5 Août 2019
Clôturé : MATLAB Answer Bot le 20 Août 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 commentaires
Neuropragmatist
Neuropragmatist le 5 Août 2019
What is the error?
Srivardhan Gadila
Srivardhan Gadila le 8 Août 2019
Can you please mention what is the exact error you are getting and at what line?

Réponses (1)

Walter Roberson
Walter Roberson le 8 Août 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.

Cette question est clôturée.

Community Treasure Hunt

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

Start Hunting!

Translated by