Info

This question is closed. Reopen it to edit or answer.

Help with saving files

1 view (last 30 days)
aurc89
aurc89 on 19 Mar 2015
Closed: MATLAB Answer Bot on 20 Aug 2021
I need help with the following operation. I have a folder, let's call it 'folder1' (directory C:\Users\Desktop\folder1), where I have the following .dat files: tryA, tryA_0, tryA_1, tryA_2,...,tryA_n , tryB, tryB_0, tryB_1, tryB_2,...,tryB_n (n is an arbitrary integer). They all have same dimension MxN double . What I need is to create and save all the matrices tryAd=tryA-tryB, tryA_0d=tryA_0-tryB_0, tryA_1d=tryA_1-tryB_1,...,tryA_nd=tryA_n-tryB_n in the same folder, once I have loaded just the two files tryA and tryB . Of course they will be .dat files with same dimension MxN. There should be a way to recognize files starting with 'tryA' and 'tryB' and ending with '_0','_1' and so on. Can you please suggest me a code to do this operation, with the variables I have indicated? Thank you very much!

Answers (1)

Konstantinos Sofos
Konstantinos Sofos on 19 Mar 2015
Edited: Konstantinos Sofos on 19 Mar 2015
Hi,
I think that List folder contents and File name processing from directory listing would be a could start for you.
I use this method all the time (replace <folder> and expression string> accordingly, and you could change the directory search string to be more specific/general if required:
% Get list of *.dat files
fnames = dir(fullfile(<folder>, '*.dat'));
% Only keep the file names - I don't care about the other data
fnames = {fnames.name}.'; % I like to ensure all vectors are columns
% Get indices of file names matching regular expression
FIND = @(str) cellfun(@(c) ~isempty(c), regexp(fnames, str, 'once'));
% Load data
fnames_subset = fnames(FIND(<regular expression string>));
Regards
  2 Comments
Konstantinos Sofos
Konstantinos Sofos on 19 Mar 2015
Edited: Konstantinos Sofos on 19 Mar 2015
replace folder with C:\Users\Desktop\folder1 and expression string with '_1' just as an example. Then fnames_subset contains the files that you want first to load and then to substruct. If for example you set expression string with 'tryA' you will have all tryA*.dat files into fnames_subset.

Community Treasure Hunt

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

Start Hunting!

Translated by