Move file into folder

1 vue (au cours des 30 derniers jours)
viet le
viet le le 31 Août 2016
Modifié(e) : Guillaume le 1 Sep 2016
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

Réponse acceptée

Guillaume
Guillaume le 31 Août 2016
Modifié(e) : Guillaume le 31 Août 2016
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 commentaires
viet le
viet le le 1 Sep 2016
Modifié(e) : viet le le 1 Sep 2016
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 le 1 Sep 2016
Modifié(e) : Guillaume le 1 Sep 2016
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);

Connectez-vous pour commenter.

Plus de réponses (2)

michio
michio le 31 Août 2016
How's trying
filename = ['Slice_',num2str(i),'.png'];
movefile(filename,newdir);
instead of
movefile('Slice_(i),view%2d',ii,jj')
  1 commentaire
viet le
viet le le 1 Sep 2016
it is working. thank

Connectez-vous pour commenter.


Thorsten
Thorsten le 31 Août 2016
Modifié(e) : Thorsten le 31 Août 2016
Use the functional syntax of print to print directly to the dir
print('-dpng', fullfile(newdir, ['Slice_' num2str(i) '.png']))
  1 commentaire
viet le
viet le le 1 Sep 2016
it is helpful.thax

Connectez-vous pour commenter.

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by