Save files to folder loop won't save the .txt files
1 次查看(过去 30 天)
显示 更早的评论
Hi there,
I'm currently trying to use a script to basically read in .txt files from a spectrometer (simply 2 columns, one wavenumbers and the other intensity counts)
It should loop through a specified folder to find every .txt file (a range of 10 samples), run the Savitsky-Golay smoothing filter on them with specified order (that can be changed to the user's desire) and then save the new smoothed sample data to a folder named the same, but with Savitsky-Golay added on to the end of the folder name. The new files will have '_SMOOTHED' added on to the oname of the original .txt files to distinguish between them.
The problem I have is that when I run the script, it creates this Savitsky-Golday folder, and performs everything as it should, but does not actually save the new .txt files into the folder. I did a full search to see if the new .txt files were being saved elsewhere, but it seems not. So the issue is, the folder is being created, but the data is not being saved in it after it has been smoothed. Any help would be greatly appreciated - I've provided the code below.
% location of data files
data_path = 'C:\....\Desktop\Fat-L100T10-785';
% create search string to pick up all txt files in folder
search_string = strcat(data_path, '\*.txt');
% find all files matching search string
files = dir(search_string);
% set the polynomial order for Savitzky-Golay
order = 5;
% specify where to save smoothed files
new_path = strcat(data_path, 'Savitzky-Golay/');
if ~exist(new_path, 'dir')
mkdir(new_path);
end
for i = 1:numel(files)
%% Raw Data Files
%
filename = files(i).name;
% open raw data
data = fopen(strcat(data_path, filename), 'r');
% format of data to extract (%f is a floating point decimal/double)
formatspec = '%f';
% extract out the data and save into a single array
% there probably IS a better way to do this but i couldn't figure it out
D = fscanf(data, formatspec);
% data saves as wave number in odd rows and intensity count in even rows
% extract out the odd rows and save to separate array
wave_number = flipud(D(1:2:end-1,:));
% extract out the even rows and save to separte array
count = flipud(D(2:2:end,:));
%% Run Savitzky-Golay Algorithm
% run the smoothign algorithm with the Savitzky-Golay method
smoothed_data = smoothdata(count, 'sgolay', 'degree', order, 'SmoothingFactor', 0.03);
% NB SmoothingFactor (0-1) specifies the window used for the algorithm.
% a value closer to 0 makes the window smaller and allows you to reduce the
% order of the polynomial. This is the only way I could figure out how to
% control the frame length. If you increase the window to closer to one,
% you need to increase the order of the polynomial also
%% Save Data
% Create table of data with variable names
T = table(wave_number, count, 'VariableNames', { 'RamanWaveNumber', 'IntensityCount'} );
% define Filename
new_filename = strcat(new_path, erase(filename, '.txt'), '_SMOOTHED.txt');
% save data to text file
writetable(T, new_filename)
end
4 个评论
Guillaume
2019-7-20
Ah, well, "The problem I have is that when I run the script, it creates this Savitsky-Golday folder, and performs everything as it should" is completely different from my code throws an error. Of course, if you get an error, the code won't do what it's meant to do. It just stops.
I didn't check the bit where you open the file since I assumed it didn't throw an error. As dpb correctly points out you build the filename wrong so you actually never open the file. So, use fullfile everywhere to make sure that you don't have problems with file separators. See dpb's answer.
采纳的回答
dpb
2019-7-20
% location of data files
data_path = 'C:\....\Desktop\Fat-L100T10-785';
% create search string to pick up all txt files in folder
search_string = strcat(data_path, '\*.txt');
% find all files matching search string
files = dir(search_string);
...
for i = 1:numel(files)
%% Raw Data Files
data = fopen(strcat(data_path,files(i).name), 'r');
...
As Guillaume says, you're not using fullfile and so you missed the file separator in creating the filename to try to open by doing straight string catenation above...and since you didn't incorporate any error-checking, you didn't discover the problem at the time...
% location of data files
data_path = 'C:\....\Desktop\Fat-L100T10-785';
% create search string to pick up all txt files in folder
search_string = strcat(data_path, '\*.txt');
% find all files matching search string
files = dir(search_string);
% set the polynomial order for Savitzky-Golay
...
for i = 1:numel(files)
filename = files(i).name;
% open raw data
[data,msg] = fopen(fullfile(data_path, filename), 'r');
if data<0, error('msg'), end
...
Similarly, later on, use the fileparts function to retrieve file base name and build new filename...
...
% define Filename
[~,filename]=fileparts(files(i).name); % retrieve base filename less extensioni
new_filename=fullfile(new_path,[filename '_SMOOTHED.txt']); % and build the new qualified filename
...
3 个评论
dpb
2019-7-20
编辑:dpb
2019-7-20
Good catch @G; missed noticing that.
The code becomes more generally usable as simply changing the wildcard string will let it work for .csv files, say, with few other changes. More generally, if were to specify the input and output file extensions as variables, then it would be almost totally generic.
ASIDE EDITORIAL COMMENT:
I've thought all along TMW ought to supply another function along with fullfile and fileparts which specifically adds an extension to the filename to avoid needing to write the specific catenation as above.
With the new string class one could use the "+" operator which is a little simpler, but since dir still (and probably always will owing to compatibility) returns a character string, the need to cast to string pretty much negates any gain.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!