Move file into folder

조회 수: 1 (최근 30일)
viet le
viet le 2016년 8월 31일
편집: Guillaume 2016년 9월 1일
I want to move a created file into created folder. Where is wrong in my code:
for ii=0:10:360
for jj=0:10:90
%%%create a multiple folder
newdir=sprintf('view%2d',ii,jj);
mkdir(fullfile(newdir));
%rotate masks
for i=1:360
rotate(aa,[0,0,1],1);
% print figure with rotate
eval(['print -dpng Slice_' num2str(i) '.png']);
%move figure into folder
movefile('Slice_(i),view%2d',ii,jj')
% pause time
pause(2);
end
end
end

채택된 답변

Guillaume
Guillaume 2016년 8월 31일
편집: Guillaume 2016년 8월 31일
newdir = sprintf('view%2d', ii, jj);
Is it really the intent that the format string only has one input, yet you pass two inputs to sprintf? You will end up with the string 'view10view15', if ii = 10 and jj = 15. If that really is the intent, then a comment saying so should be added, or better, make it explicit that it's wanted:
newdir = sprintf('view%2dview%2d', ii, jj);
Next,
mkdir(fullfile(newdir));
is a bit unconventional. You normally pass at least two parameters to fullfile. As it is, in my version of matlab this will normalise the folder separator, but that's not a documented behaviour.
Next,
eval(['print -dpng Slice_' num2str(i) '.png'])
Ewww! Why the eval? It serves absolutely no purpose other than slowing down the code, making it impossible to debug and hiding all the syntax errors. How about:
printf('-dpng', sprintf('Slice_%d.png', i));
Finally,
movefile('Slice_(i),view%2d',ii,jj')
since when does movefile takes a number as the second and third argument? That line makes absolutely no sense. Perhaps you meant:
movefile(sprintf('Slice_%d.png', i), newfolder);
But anyway, why not directly create the file in the correct folder:
filename = sprintf('Slice_%d.png', i);
print('-dpng', fullfile(newfolder, filename)); %and even better would be to pass the figure handle to print.
%no movefile needed, the file is already where you want it.
  댓글 수: 2
viet le
viet le 2016년 9월 1일
편집: viet le 2016년 9월 1일
thank for your help. it is helpful. can I ask you a bit more? after created figures in the folder. I want to read figures again to calculate, and then save .xls file in the folder created. how to do this? this is my code, but it is not working:
for ii=0:10:360
for jj=0:10:90
view(ii,jj);
%%%create a multiple folder
newdir=sprintf('view%2dview%2d',ii,jj);
mkdir(fullfile(newdir));
%rotate masks
for i=1:36
rotate(aa,[0,0,1],1);
% print figure with rotate
filename = sprintf('Slice_%d.png', i);
print('-dpng', fullfile(newdir, filename));
pause(2);
end
%%%%%%%%calculate red pixels and save in excel file
%%%%%%%%%%%%%%%
for i = 1 : 36
filename=['D:\Matlab code RMC\zzzz\newdir\Slice_ ' num2str(i,'%01d') ];
rgbImage = imread(filename);
imshow(rgbImage);
redChannel = rgbImage(:,:, 1);
greenChannel = rgbImage(:,:, 2);
blueChannel = rgbImage(:,:, 3);
greenness = mean2(greenChannel);
% Extract the individual red, green, and blue color channels.
redPixels = redChannel == 255 & greenChannel == 0 & blueChannel == 0;
redArea(i) = sum(redPixels(:));
end
%I want to save .xls into the folder created view(ii)view(jj)
filename='test.xlsx';
A=[redArea];
xlswrite(filename,A);
Guillaume
Guillaume 2016년 9월 1일
편집: Guillaume 2016년 9월 1일
Please, spend some time reading the documentation of sprintf and fullfile.
To read your image:
rgbImage = imread(fullfile(newdir, filename)); %no point in recreating the filename string
To save your excel file:
xlswrite(fullfile(newdir, 'test.xlsx'), redArea);

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

추가 답변 (2개)

michio
michio 2016년 8월 31일
How's trying
filename = ['Slice_',num2str(i),'.png'];
movefile(filename,newdir);
instead of
movefile('Slice_(i),view%2d',ii,jj')
  댓글 수: 1
viet le
viet le 2016년 9월 1일
it is working. thank

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


Thorsten
Thorsten 2016년 8월 31일
편집: Thorsten 2016년 8월 31일
Use the functional syntax of print to print directly to the dir
print('-dpng', fullfile(newdir, ['Slice_' num2str(i) '.png']))
  댓글 수: 1
viet le
viet le 2016년 9월 1일
it is helpful.thax

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

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by