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')

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']))

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by