3 loops using sprintf to change directories doesn't quite work

2 次查看(过去 30 天)
Hello everyonem
I would like to do a code that write a txt procedure for another software, that go through a multiples folder to find files.
I got k main folders with each 2 subfolders (j and i)
The k and j variables are set and their value are correct in the workspace. But lines sprintf for k and j loop don't modify topo and dir1 and strut and dir2 names, so i don't have the good directory, example :
I have this
G:\Matthieu\Nomenclature\topo\strut\file1\file1.bdf
...
G:\Matthieu\Nomenclature\topo\strut\file9\file9.bdf
instead of that
G:\Matthieu\Nomenclature\topo1\strut3\file1\file1.bdf
...
G:\Matthieu\Nomenclature\topo1\strut3\file9\file9.bdf
Only file is modified. But i have the right amount of lines.
The code is below, I would appreciate any help! Also, if you think it should be optimised let me know!
Thanks a lot, Matt
clc
clear all
folder = sprintf('G:\\Matthieu\\Nomenclature\\script');
proc_array1 = dir('G:\\Matthieu\\Nomenclature\\topo*');
proc_array2 = dir('G:\\Matthieu\\Nomenclature\\topo1\\strut*');
proc_array3 = dir('G:\\Matthieu\\Nomenclature\\topo1\\strut0\\file*');
%size of folders check
num_models1=size(proc_array1, 1)
num_models2=size(proc_array2, 1)
num_models3=size(proc_array3, 1)
%create loop sizes
n_proc1 = size(proc_array1, 1);
n_proc2 = size(proc_array2, 1);
n_proc3 = size(proc_array3, 1);
%create txt
proc_f = fullfile(folder , 'fullproc.proc');
fid1 = fopen(proc_f,'w');
%txt constant strings to be printed
model_creation='*new_model\n yes \n';
yes='yes';
n='\n';
import='*import nastran ';
exec_proc='*exec_procedure G:\\Matthieu\\Nomenclature\\script\\basicprocedure.proc \n';
save_as='*set_save_formatted off *save_as_model ';
save='save_model';
base_dir='G:\\Matthieu\\Nomenclature';
slash='\\';
%loop 1 create topo1, topo 2
for k= 1:n_proc1
topo=sprintf('topo%k',k);
dir1=[base_dir,slash,topo,slash]
%loop 2 create strut0 to 4
for j= 1:n_proc2
strut=sprintf('strut%j',j);
dir2=[strut,slash]
%loop 3 create file1 to 10 and marc1 to 10
for i= 1:n_proc3
file=sprintf('file%i',i);
imp_name=sprintf('file%i.bdf',i);
exp_name=sprintf('marc%i.mud ',i);
full_dir=[dir1, dir2, file,slash]
total=[model_creation,n,save_as,full_dir,exp_name,n,yes,n,import,full_dir,imp_name,n,exec_proc,save,n];
fprintf(fid1,total);
end
end
end
fprintf(fid1,'*quit yes \n');
fclose(fid1);
% total=[model_creation,n,save_as,exp_name,n,imp_dir,imp_name,n,exec_proc,n,save,n,n];

采纳的回答

Bjorn Gustavsson
Bjorn Gustavsson 2021-11-4
Have a good look at the help and documentation of fullfile. That function is made to help you generate full filenames with absolute or relative paths, in addition it will be system-independent such that your function will work in both microsoft's \-systems and UNIX's/Linux's and Apple's /-systems, and you will not have to manually concatenate directory-names and make sure that you get the right number of separators in (this is a great benefit as far as I'm concerned).
HTH

更多回答(2 个)

Matthieu Aoun
Matthieu Aoun 2021-11-4
Okay thanks for the help!
By the way, i just solved my problem : here is the new loop :
topo=sprintf('topo%i',k)
strut=sprintf('strut%i',j)
%instead of
topo=sprintf('topo%k',k)
strut=sprintf('strut%j',j)
I thought %i call i but %i means integer! Ahah
  3 个评论
Bjorn Gustavsson
Bjorn Gustavsson 2021-11-4
If it is your first time really doing matlab-programming there are a couple of useful hints:
Look for answers here, the search function is rather good.
Browse the help and documentation-pages, that gave me a decent overview of what functions were available.
Make use of the lookfor function - if you have a good keyword lookfor returns all functions with that keyword in the first line of the help - even if the function with the seemingly best match is not solving your problem its help-section might still have a function listed in the "see also"-section that does solve (most of) your problem.
Make good use of the file exchange - it is full of functions and toolboxes that can help you greatly.

请先登录,再进行评论。


标签

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by