Azzera filtri
Azzera filtri

Use file name to save .mat files

36 visualizzazioni (ultimi 30 giorni)
A-Rod
A-Rod il 1 Lug 2024 alle 17:28
Commentato: Voss il 2 Lug 2024 alle 12:51
Hello community.
I'd like to have your support to find a soluion.
I have several '.dat' files, I'm usinng mdfimport to export the variables I need and then save them into '.mat' format.
my code:
clear all;
clc;
[FileName,PathName] = uigetfile('*.dat','Select the .dat-file(s)','MultiSelect', 'on');
if class(FileName) == char('cell');
FileName = FileName';
end
if class(FileName)==char('char');
FileName = {FileName};
end
%========================================================================%
V = {
['EnvT_t']
['CtT_flgHeal']
['CtT_flgEna']
['Epm_nEng']
['CTM_Delta']
['CTM_Flag']
['CTM_Sum']
};
V=V';
for k=1:length(FileName)
%----------------------------------------------------------------------
progress = ['Working on file ' int2str(k) ' of ' int2str(length(FileName)) '...'];
disp(progress);
disp(FileName(k));
LoadPath = char(strcat(PathName, FileName(k)));
mdfimport(LoadPath,[],V, 'resample_1');
save(['@' num2str(k) '.mat'])
end
let's say I have below data:
Carr1.dat
Carr2.dat
Carr3.dat
Carr4.dat
my code will go trhoug each .dat file, will extract defined variables and then it will save it as follows:
@1.mat
@2.mat
@3.mat
@4.mat
how can I use save command to keep original file name? I'm looking to get this:
Carr1.mat
Carr2.mat
Carr3.mat
Carr4.mat
I've tryied different things but always get an error.
as always your feedback will be highly appreciated

Risposta accettata

Voss
Voss il 1 Lug 2024 alle 19:09
Modificato: Voss il 1 Lug 2024 alle 19:16
[FileName,PathName] = uigetfile('*.dat','Select the .dat-file(s)','MultiSelect','on');
if isnumeric(FileName)
return
end
FileName = cellstr(FileName);
V = { ...
'EnvT_t' ...
'CtT_flgHeal' ...
'CtT_flgEna' ...
'Epm_nEng' ...
'CTM_Delta' ...
'CTM_Flag' ...
'CTM_Sum' ...
};
[~,fn,~] = fileparts(FileName);
FileName_mat = cellstr(strcat(fn,'.mat'));
N = numel(FileName);
for k = 1:N
fprintf(1,'Working on file %d of %d ...\n%s\n',k,N,FileName{k});
LoadPath = fullfile(PathName,FileName{k});
mdfimport(LoadPath,[],V, 'resample_1');
SavePath = fullfile(PathName,FileName_mat{k});
save(SavePath)
end
  5 Commenti
A-Rod
A-Rod il 2 Lug 2024 alle 11:54
this is great, I appreciate your time to help.
Voss
Voss il 2 Lug 2024 alle 12:51
You're welcome

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Data Import and Analysis in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by