フィルターのクリア

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.
Voss
Voss 2024 年 7 月 2 日 12:51
You're welcome

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Import and Analysis についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by