Clear Filters
Clear Filters

Use file name to save .mat files

36 views (last 30 days)
A-Rod
A-Rod on 1 Jul 2024 at 17:28
Commented: Voss on 2 Jul 2024 at 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

Accepted Answer

Voss
Voss on 1 Jul 2024 at 19:09
Edited: Voss on 1 Jul 2024 at 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 Comments
A-Rod
A-Rod on 2 Jul 2024 at 11:54
this is great, I appreciate your time to help.
Voss
Voss on 2 Jul 2024 at 12:51
You're welcome

Sign in to comment.

More Answers (0)

Categories

Find more on Data Import and Analysis in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!