Use file name to save .mat files

36 次查看(过去 30 天)
A-Rod
A-Rod 2024-7-1,17:28
评论: Voss 2024-7-2,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

采纳的回答

Voss
Voss 2024-7-1,19:09
编辑:Voss 2024-7-1,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 个评论
A-Rod
A-Rod 2024-7-2,11:54
this is great, I appreciate your time to help.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Import and Analysis 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by