saving the output arrays in for loop

1 view (last 30 days)
eman
eman on 30 Sep 2014
Answered: Image Analyst on 30 Sep 2014
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

Answers (3)

Jan
Jan on 30 Sep 2014
Edited: Jan on 30 Sep 2014
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 Comments
eman
eman on 30 Sep 2014
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 on 30 Sep 2014
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.

Sign in to comment.


Andrei Bobrov
Andrei Bobrov on 30 Sep 2014
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 on 30 Sep 2014
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?

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!