How to save text files to a different folder than read?

I have a lot of files that I need to process of the form "abcdef - ###.txt" where ### can be anywhere from 4 to 9 sequence of random numbers. I have some code that is reading each file using dlmread in a for loop. Each file will get modified in some way and eventually will be left with multiple matrices that I want to output each to a new .txt file in a separate folder from where I'm getting the data from that looks like "abcdef - ###_x.txt", "abcdef - ###_x.txt" etc... where x is 1:number of matrices extracted
I was able to use sprintf('%s_%d,filename,x) to get dynamic variable naming for the matrices but I can't figure out how to write (dlmwrite is what I tried) each matrix to a new file dynamically.
myDirectory = uigetdir; %open directory
myFiles = dir(fullfile(myDirectory,'*.txt')); %get files in folder
for k = 1:length(myFiles) %loop through each file in myFiles
baseFileName = myFiles(k).name;
filename = fullfile(myDirectory, baseFileName);
fprintf(1, 'Now reading %s\n', filename);
data=dlmread(filename,'',1,0); %build data matrix from file
filename=filename(1:end-4); %remove .txt from filename string
[rows,cols] = size(data); %
data_ext = data(:,[1,2,4]); %Extract time, flowrate (AU), concentration (AU)
numInjections = 0;
prevInjecting = false;
injectionData = []; %Empty injection array
for i=1:rows %index from 1 to #rows
currentFlow = data_ext(i,2); %flow at index i
if currentFlow > 5 && ~prevInjecting %is flow @ i > 5 and not previously?
numInjections = numInjections+1; %increase injection count
prevInjecting = true; %injection status: ON
injectionData(1,:) = data_ext(i,:); %%begin building array
elseif currentFlow > 5 && prevInjecting %if not beginning of injection
injectionData = [injectionData;data_ext(i,:)]; %%amend injection array with rows
elseif currentFlow <= 5 && prevInjecting %end of injection
%
injectionData(:,2)=injectionData(:,2).*(0.1/1024); %convert from AU to L/min
injectionData(:,3)=injectionData(:,3).*0.005-.1584; %convert from AU to g/L
%take this array and count with # of injections
filename_inj = sprintf('%s_%d.txt',filename,numInjections);
%dlmwrite(filename_inj,injectionData,'delimiter','\t');% write data to .txt file
injectionData = []; %reset injection data array,
prevInjecting = false; %injection status: OFF
else
prevInjecting = false; %injection status: OFF
end
end
end

1 Comment

I'm not sure, if I understand your question correctly. So right now your program is working as expected, but all files are being saved to MATLAB current folder instead of some different - user selected - folder, right?
If that's true, you have to get the path to output directory. At the beginning of your script you can add:
%select output directory
myOutputDirectory = uigetdir([],'Now select output directory')
And then you can build full path of output file using fullfile:
dlmwrite(fullfile(myOutputDirectory, filename_inj),injectionData,'delimiter','\t');

Sign in to comment.

Answers (0)

Categories

Asked:

on 13 Dec 2017

Edited:

on 14 Dec 2017

Community Treasure Hunt

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

Start Hunting!