Borrar filtros
Borrar filtros

Use file name to save .mat files

36 visualizaciones (últimos 30 días)
A-Rod
A-Rod el 1 de Jul. de 2024 a las 17:28
Comentada: Voss el 2 de Jul. de 2024 a las 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

Respuesta aceptada

Voss
Voss el 1 de Jul. de 2024 a las 19:09
Editada: Voss el 1 de Jul. de 2024 a las 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 comentarios
A-Rod
A-Rod el 2 de Jul. de 2024 a las 11:54
this is great, I appreciate your time to help.
Voss
Voss el 2 de Jul. de 2024 a las 12:51
You're welcome

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Import and Analysis en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by