saving the output arrays in for loop

1 次查看(过去 30 天)
I need to save the value of TrnPatches for every iteration in for loop and display it How can I do that?
Here is the code:
clc;
clear all;
close all
TrnImgPath='G:\Master_Students\Iman\UIUC\PNGImages\TrainImages\';
TrnPatches=[];
PatchWidth=8;
PatchHeight=8;
NImgs=100;
for i=1:NImgs
I=imread([TrnImgPath 'pos-' num2str(i) .PNG']);%Reading positive Pictures
I=im2double(I);%Convert image to double precision
TrnPatches=[TrnPatches;im2col(I,[PatchHeight,PatchWidth],'sliding')'];
%why the size changes
% imshow(TrnPatches,[]);
disp(i);
disp(TrnPatches);
end

回答(3 个)

Jan
Jan 2014-9-30
编辑:Jan 2014-9-30
Insert these lines:
filename = fullfile(TrnImgPath, sprintf('Picture%d.mat', i));
save(filename, TrnPatches);
By the way: You find a lot of help when you ask your favorite internet search engine for "Matlab save files in a loop". The forum is more powerful when you take the chance to examine the already discussed topics.
  2 个评论
eman
eman 2014-9-30
Thanks after inserting the two lines and running the code, when I clicked filename at work space it is empty. what is the problem?
Image Analyst
Image Analyst 2014-9-30
Here, look at this. Then you'll be able to find out whether there's a problem with i, or filename or something else. As long as i is not empty and TrnIMgPath is not empty, then filename cannot be empty. I think if filename were empty, then save() would throw an error. So I have my doubts about what you say, like you're running somewhat different code than what Jan gave. So you're doing something wrong that can be figured out by stepping through and examining variables.

请先登录,再进行评论。


Andrei Bobrov
Andrei Bobrov 2014-9-30
one way
cd G:\Master_Students\Iman\UIUC\PNGImages\TrainImages
n = dir('pos-*.PNG');
name1 = {n.name};
nn = numel(name1);
TrnPatches = cell(nn,1);
for ii = 1:nn
I = imread(name1{ii});
TrnPatches{ii} = im2col(im2double(I),[8,8],'sliding').';
end

Image Analyst
Image Analyst 2014-9-30
What are you doing with im2col? I never use that function. And why are you concatenating all the images and wanting to save an ever growing image at each iteration?

类别

Help CenterFile Exchange 中查找有关 Images 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by