Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Save matrix in a loop

Subject: Save matrix in a loop

From: shazwani kamarudin

Date: 15 Jul, 2013 08:07:10

Message: 1 of 2

hello~

this is my code to segment all images in testing folder..the problem is,i want to save each matrix data in different .mat file using a loop..i dont know how to do it

clear all
srcFiles = dir('\MSc AIA\amazon images\1-1000\testing\*.jpg');
for i = 1 : length(srcFiles)
    
filename = strcat('\MSc AIA\amazon images\1-1000\testing\',srcFiles(i).name);
I = imread(filename);
level = graythresh(I);
BW = im2bw(I,level);
pathname = '\MSc AIA\amazon images\1-1000\testing\segmented';
segment = sprintf('%d.bmp', i);
fullfilename = fullfile(pathname, segment);
imwrite(BW, fullfilename);

end

Subject: Save matrix in a loop

From: someone

Date: 15 Jul, 2013 14:38:12

Message: 2 of 2

"shazwani kamarudin" wrote in message <ks0aje$o7e$1@newscl01ah.mathworks.com>...
> hello~
>
> this is my code to segment all images in testing folder..the problem is,i want to save each matrix data in different .mat file using a loop..i dont know how to do it
>
> clear all
> srcFiles = dir('\MSc AIA\amazon images\1-1000\testing\*.jpg');
> for i = 1 : length(srcFiles)
>
> filename = strcat('\MSc AIA\amazon images\1-1000\testing\',srcFiles(i).name);
> I = imread(filename);
> level = graythresh(I);
> BW = im2bw(I,level);
> pathname = '\MSc AIA\amazon images\1-1000\testing\segmented';
> segment = sprintf('%d.bmp', i);
> fullfilename = fullfile(pathname, segment);
> imwrite(BW, fullfilename);
>
> end

% I'm not sure what"matrix data" you are trying to save,
% but if the rest of your code works,
% couldn't you just add something like:

matfile = sprintf('%d.mat', i);
matfilename = fullfile(pathname, matfile);
save(matfilename);

% to your existing for loop?

% You might also take a look at Q3.1 & 3.10
% of the MATLAB FAQ at:

http://matlab.wikia.com/wiki/FAQ

% Specifically:

<<http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F>>

<<http://matlab.wikia.com/wiki/FAQ#How_do_I_dynamically_generate_a_filename_for_SAVE.3F>>

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us