필터 지우기
필터 지우기

mixing string name and number,is my commmand correct??

조회 수: 1 (최근 30일)
farzad
farzad 2014년 10월 17일
댓글: farzad 2014년 10월 17일
Hi All
Since I just dont remember and dont know how to search for this question
would like to know
for naming the file as :
for i=1:100
delete('filename'+num2str(i))
end
is it correct ?

채택된 답변

Image Analyst
Image Analyst 2014년 10월 17일
I'd recommend you follow the FAQ: http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F because your method is not very robust. I prefer sprintf(). If you're not going to use, or can't use, dir() to get a listing of what files are there, then at least you should use exist() so you don't throw an exception that aborts your loop when the file is not found. That is what people do when they write robust code.
for k = 1 : 100 % Use k, not i (the imaginary variable)
baseFileName = sprintf('filename%d', k);
% Prepend the folder where the files live.
% If it's the current folder, then you can use folder = pwd.
fullFileName = fullfile(folder, baseFileName);
if exist(fullFileName, 'file')
% If the file exists, delete it.
delete(fullFileName);
end
end
but again, see the FAQ for better code examples than that.
  댓글 수: 4
farzad
farzad 2014년 10월 17일
and also , where do I put the .extension of the filename in the base or full file name , I mean , how should it look like ?
Adam
Adam 2014년 10월 17일
If you put the extension on the baseFileName it will the get included correctly in the fullFileName.
%d is a placeholder for an integer which gets replaced by the value of k each time round the loop (look at formatSpec in the help for sprintf for other options here if you are interested in general, but %d is the correct one for this case)

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Claude Porlier
Claude Porlier 2014년 10월 17일
Try using this !
for i=1:100
delete(strcat('filename',num2str(i)))
end
Cheers !
  댓글 수: 1
farzad
farzad 2014년 10월 17일
Thanks a lot , I still have a question that I put in the end , if you do a favor check , thanks a lot

댓글을 달려면 로그인하십시오.


farzad
farzad 2014년 10월 17일
I got a bit confused , Since I have two file types : one is like :
file1.txt.i
the other
file2i.rec
in both , the letter "i" is the changing number , so I need to know in all of the methods you have proposed , hwo should I call them, I am getting some errors
thank you very much in advance

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by