필터 지우기
필터 지우기

How to add Filename to all variables automatically?

조회 수: 2 (최근 30일)
Mert Dogan
Mert Dogan 2017년 10월 8일
댓글: Mert Dogan 2017년 10월 8일
Hi, everyone I have 127 variable in a .mat file. I want to add filename to all variables and save them a path like "File\Filename_VariableX" automatically. Thanks a lot.

채택된 답변

Walter Roberson
Walter Roberson 2017년 10월 8일
Assuming you want to process a whole directory like this:
projectdir = 'File'; %location to save into
if ~exist(projectdir, 'dir'); mkdir(projectdir); end
dinfo = dir('*.mat');
for fidx = 1 : length(dinfo)
filename = dinfo(fidx).name;
[~, basename] = fileparts(filename);
datastruct = load(filename);
fn = fieldnames(datastruct);
for nidx = 1 : length(fn)
varname = fn{nidx};
outvarname = [basename '_' varname];
outfilename = fullfile(projectdir, [outvarname '.mat']);
outstruct = struct( outvarname, datastruct.(varname) );
save(outfilename, '-struct', outstruct);
end
end
This preserves variable names inside the .mat files.
  댓글 수: 3
Walter Roberson
Walter Roberson 2017년 10월 8일
save(outfilename, '-struct', 'outstruct');
Mert Dogan
Mert Dogan 2017년 10월 8일
Thanks a lot. Walter can i ask a question ? How can i create file in a path automatically ?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Predict Remaining Useful Life (RUL)에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by