Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

Help with saving files

2 visualizaciones (últimos 30 días)
aurc89
aurc89 el 19 de Mzo. de 2015
Cerrada: MATLAB Answer Bot el 20 de Ag. de 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!

Respuestas (1)

Konstantinos Sofos
Konstantinos Sofos el 19 de Mzo. de 2015
Editada: Konstantinos Sofos el 19 de Mzo. de 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 comentarios
aurc89
aurc89 el 19 de Mzo. de 2015
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 el 19 de Mzo. de 2015
Editada: Konstantinos Sofos el 19 de Mzo. de 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