fprintf function for structuring element

2 次查看(过去 30 天)
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?

采纳的回答

Cedric
Cedric 2013-3-18
编辑:Cedric 2013-3-18
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 个评论
Cedric
Cedric 2013-3-19
编辑:Cedric 2013-3-19
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 2013-3-19
That will probably work. Good Call.

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by