How to add Filename to all variables automatically?
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Mert Dogan
il 8 Ott 2017
Commentato: Mert Dogan
il 8 Ott 2017
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.
0 Commenti
Risposta accettata
Walter Roberson
il 8 Ott 2017
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 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Predict Remaining Useful Life (RUL) in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!