Info

此问题已关闭。 请重新打开它进行编辑或回答。

Error with importdata when looping through files in folder

1 次查看(过去 30 天)
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

回答(1 个)

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

此问题已关闭。

Community Treasure Hunt

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

Start Hunting!

Translated by