Info

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

Help with saving files

2 次查看(过去 30 天)
aurc89
aurc89 2015-3-19
关闭: MATLAB Answer Bot 2021-8-20
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!

回答(1 个)

Konstantinos Sofos
Konstantinos Sofos 2015-3-19
编辑:Konstantinos Sofos 2015-3-19
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 个评论
aurc89
aurc89 2015-3-19
Thanks, but the code is not clear to me...I understood the first part but I can't figure out how to do the operation of difference I require: I need to find the files starting with tryA and tryB and subtract two by two the matrices with the same ending number (_0,_1,_2...)
Konstantinos Sofos
Konstantinos Sofos 2015-3-19
编辑:Konstantinos Sofos 2015-3-19
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