fprintf function for structuring element

2 views (last 30 days)
I'm working on a GUI that outputs user commands to a script file, and I've run into an issue programming the structuring element for performing morphological operations. As it stands, I construct the structuring element by allowing the user to choose the shape and the values for radius, length, or whatever, so the structuring element looks something like:
se = strel('disk',5);
However, I can't output that statement to the script using fprintf because it's a mix of character values and numerical values. So, how can this be done?

Accepted Answer

Cedric
Cedric on 18 Mar 2013
Edited: Cedric on 18 Mar 2013
fid = fopen ('script.txt', 'a') ;
disk = 5 ; % Defined form user input.
fprintf(fid, 'se = strel(''disk'', %g);\n', disk) ;
square = 7 ; % Defined form user input.
fprintf(fid, 'se = strel(''square'', %g);\n', square) ;
% You could also have the shape given as a string from some drop-down and
% use it as in ..
shape = 'triangle' ; % Defined from drop-down.
value = 5 ;
fprintf(fid, 'se = strel(''%s'', %g);\n', shape, value) ;
% ...
fclose(fid) ;
  9 Comments
Cedric
Cedric on 19 Mar 2013
Edited: Cedric on 19 Mar 2013
Actually if you perform
se = strel('disk',5);
img = imdilate(img, se);
what you want to do in order to save these "commands" to file is:
fprintf(fid, 'se = strel(''disk'',5);\n') ;
fprintf(fid, 'img = imdilate(img, se);\n') ;
If you had the shape defined by some control in your GUI and saved in a string variable named shape, you could do the following:
fprintf(fid, 'se = strel(''%s'',5);\n', shape) ;
fprintf(fid, 'img = imdilate(img, se);\n') ;
In any case, you want the string 'se' to be in the format of FPRINTF, and not the output of disp(se), as it is the se that will be created when the saved script will be run that must be passed to IMDILATE, and not "some representation" of the current se.
Caleb
Caleb on 19 Mar 2013
That will probably work. Good Call.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!