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?
0 件のコメント
採用された回答
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
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.
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!