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

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

How to publish text and pictures in a loop

Asked by per isakson on 7 Jul 2012

I want to make a simple catalog over screen clips, which I captured with the Windows 7 Snipping Tool. That's simple with a for-loop, I thought. That would output text, picture, text, picture, etc. However, the script below produces a block of text followed by the pictures. (R2012a).

Next, I included the text in the figure with the function, title. However, I have set a max width as an publish option. In many cases that made the text unreadable small.

Using the errordlg to show the text is not attractive.

Have I missed an option in publish?

Is there a simple work around?

%% Snipping Tool clips
file_spec   = fullfile( folder_spec, '*.png' );
sad         = transpose( dir( file_spec ) );
%%
for sa = sad
    fprintf( '%s | %8u | %s\n', datestr(sa.datenum,31), sa.bytes, sa.name )
    ... create a figure with an axes 
    pic = imread( fullfile( folder_spec, sa.name ), 'png' );
    imagesc( pic )
end

1 Comment

Walter Roberson on 7 Jul 2012

No drawnow() ?

per isakson

Products

2 Answers

Answer by Sean de Wolski on 29 Jul 2013
Accepted answer
%% 

is a code secton break. You can find more information on it here:

http://www.mathworks.com/help/releases/R2013a/matlab/matlab_prog/run-sections-of-programs.html#brqxeeu-285

You may also be interested in snapnow() to force publish to take a snapshot.

doc snapnow

0 Comments

Sean de Wolski
Answer by per isakson on 8 Jul 2012
Edited by per isakson on 8 Aug 2013

I've found the answer, which raises a new question. How come I missed the importance the position of "%%" relative to the loop?

A "%%" need to be inside the loop to "flush the output buffer". The code below does it.

%% Snipping Tool clips
file_spec   = fullfile( folder_spec, '*.png' );
sad         = transpose( dir( file_spec ) );
for sa = sad
%%
    fprintf( '%s | %8u | %s\n', datestr(sa.datenum,31), sa.bytes, sa.name )
    ... create a figure with an axes 
    pic = imread( fullfile( folder_spec, sa.name ), 'png' );
    imagesc( pic )
end

.

Cont.

The code below illustrates the role of cell break, %%, inside the loop. Only Test 1 produces the result

  • text,picture, text,picture, text,picture
%%
rgb = imread('ngc6543a.jpg');
%
%% Test 1
for ii = 1 : 3
%%    
    fprintf( 'Picture: %u', ii )
    image(rgb);
end
%% Test 2
for ii = 1 : 3  
    fprintf( 'Picture: %u\n', ii )
    image(rgb);
end
%% Test 3
for ii = 1 : 3  
    fprintf( 'Picture: %u\n', ii )
    figure, image(rgb);
end
%% Test 4
for ii = 1 : 3  
    fprintf( 'Picture: %u\n', ii )
    figure, image(rgb);
    drawnow()
end

.

2013-08-08: With R2013a and snapnow, this example produces the desired result

  • text,picture, text,picture, text,picture
%% Test with snapnow
for ii = 1 : 3  
    fprintf( 'Picture: %u\n', ii )
    figure, image(rgb);
    snapnow()
end

Since when has snapnow been around? Sean, thanks for making me aware of it.

8 Comments

per isakson on 19 Jul 2013

Probably, in test 2 the images are overwritten before they are captured. I guess, the image captured is the last one.

I can think of one somewhat clumsy work-around. Make an m-function with a for-loop, which writes an intermediate m-file, which contains

    %%    
    fprintf( 'Picture: %u', 1 )
    image(rgb1);
    %%    
    fprintf( 'Picture: %u', 2 )
    image(rgb1);
    %%    
    fprintf( 'Picture: %u', 3 )
    image(rgb3);

Then publish this intermediate file.

francesco on 22 Jul 2013

Yes, I think this is the only to overcome the problem. Thank you very much for your help ! Best regards

Peter on 29 Jul 2013

And something else to watch out for: Following this answer I discovered (2012b) that a try catch block around the section ment to flush the figures is a problem. For me, I had to remove the try catch, otherwise the %% had no effect in within the loop.

per isakson

Contact us