loop save new name and save .mat
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
This is main function
function [] = READIMAGEFORAREA ()
srcFiles = dir('C:\Users\xzaa\Documents\MATLAB\bwwalk2\*.jpg'); % the folder in which ur images exists
j=1;
for i = 1 : length(srcFiles)
filename = strcat('C:\Users\xzaa\Documents\MATLAB\bwwalk2\',srcFiles(i).name);
I = imread(filename);
imshow(I);
drawnow;
AREAPICTURE (I,j);
j=j+1;
end
end
and 2nd function
function [] = AREAPICTURE (BW,i)
BWAREA = bwarea(BW);
j=num2str(i);
save('AREA.mat',strcat('BWAREA00'),j);
end
I want to save in file .mat and save many object in .mat in code that error loop in program
this error Error in AREAPICTURE (line 4) save('AREA.mat',strcat('BWAREA00'),j);
Error in READIMAGEFORAREA (line 9) AREAPICTURE (I,j);
1 Kommentar
Jan
am 24 Jan. 2017
Bearbeitet: Jan
am 24 Jan. 2017
Please read and apply: http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup . Then edit the question and insert the complete error message: currently we can only see, where the problem occurres, but not which problem.
Antworten (2)
Jan
am 24 Jan. 2017
Your code:
function [] = AREAPICTURE (BW,i)
BWAREA = bwarea(BW);
j = num2str(i);
save('AREA.mat',strcat('BWAREA00'),j)
This is equivalent to the call (when i=7):
save AREA.mat BWAREA00 7
and tries to save the variables "BWAEREA00" and "7" to the file called "AREA.mat". Due to the lack of comment we have to guess the intention of this code. A bold guess:
function AREAPICTURE(BW, index) % Use "i" for the imaginary unit only
BWAREA = bwarea(BW);
save(sprintf('BWAREA%03d.mat', index), 'BWAREA')
3 Kommentare
Walter Roberson
am 25 Jan. 2017
function AREAPICTURE(BW, index) % Use "i" for the imaginary unit only
BWAREA = bwarea(BW);
field = sprintf('BWAREA%d', index);
datastruct.(field) = BWAREA;
save('AREA.mat', '-struct', 'datastruct', '-append')
If you were to use a -v7.3 .mat file then you could handle this a bit more cleanly using matFile()
5 Kommentare
Walter Roberson
am 25 Jan. 2017
Possibly the problem occurs while reading the 21st image. You should put in some debugging:
function [] = READIMAGEFORAREA ()
projectdir = 'C:\Users\xzaa\Documents\MATLAB\bwwalk2'
srcFiles = dir( fullfile(projectdir, '*.jpg') );
IGNORETHISVARIABLE = []; %the file needs to exist
save( 'AREA.mat', 'IGNORETHISVARIABLE');
for index = 1 : length(srcFiles)
filename = fullfile(projectdir, srcFiles(index).name );
fprintf('about to read image #%d: "%s"\n', index, filename);
I = imread(filename);
fprintf('image #%d read\n', index);
imshow(I);
drawnow;
AREAPICTURE(I, index);
end
function AREAPICTURE(BW, index)
BWAREA = bwarea(BW);
field = sprintf('BWAREA%d', index);
datastruct.(field) = BWAREA;
fprintf('about to save field %s\n', field);
save('AREA.mat', '-struct', 'datastruct', '-append')
fprintf('saved field %s\n', field);
Siehe auch
Kategorien
Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!