필터 지우기
필터 지우기

Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Help with saving files

조회 수: 1 (최근 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