Convert *.lvm automatically in *.txt

56 views (last 30 days)
Antonia Schelte
Antonia Schelte on 21 Jun 2016
Answered: Walter Roberson on 24 Aug 2021
Hi. How can I automatically convert in a loop 25 files like A_12.lvm, A_13.lvm, etc. until A_36.lvm with *.lvm extention into *.txt extention ?
Many thanks for help.

Answers (2)

NE on 23 Jul 2016
Edited: NE on 24 Aug 2021
Is there some .lvm files you don't want to convert ? And by convert do you mean only changing the extension ?
If yes here is something you can try (it convert all the .lvm files that begin with "A_" in a designated folder):
% Ask the user for the folder containing the LVM files
LVM_files_path = uigetdir(cd,'Select the FOLDER of the LVM files to rename');
% Or put the path directly in Matlab (you will need to uncomment the line)
%LVM_files_path = '.....';
% Get all LVM files in the selected folder
LVM_files = dir(fullfile(LVM_files_path,'*.lvm'));
% Loop on all the files
cpt_files_renamed = 0; % init
for id = 1:length(LVM_files)
% Get only the file name
[~,name,~]= fileparts(LVM_files(id).name);
if strcmp(name(1:2),'A_') % Check if the name of the file begins w/ "A_"
num = str2double(name(3:end));
if ~isnan(num)
% rename (the only solution is to use "movefile")
movefile(LVM_files(id).name, [name '.txt']);
cpt_files_renamed = cpt_files_renamed + 1;
% display a message to say that the files have been renamed
msgbox([num2str(cpt_files_renamed) ' files renamed'],'Success');

Walter Roberson
Walter Roberson on 24 Aug 2021
for K = 1 : 36
basename = sprintf('A_%d', K);
filename = [basename '.lvm'];
outfile = [basename '.mat'];
clear data
data.(basename) = lvm_import(filename, 1);
save(outfile, '-struct', 'data');
This code saves the data in a .mat file named the same thing as the input file (without the extension), as a variable with the same name as the input file (without an extension.)

Community Treasure Hunt

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

Start Hunting!

Translated by