Write a function drawBox which draws boxes on the screen

I got a task but I am confused. This is My task The function is called with a character (ch) as the first argument and two positive integers which represent the width and the height of the box to be drawn. It automatically draws a box with the character (ch) by the specified width and height. Can Anyone help me out with the task.

1 件のコメント

Abu Yamen
Abu Yamen 2016 年 12 月 3 日
function []= drawBox('s',w,h)
ch =input('ch-ch ','s'); w =input('width : '); h =input('hight : ');
x = repmat(ch, h, w); x(2:end-1, 2:end-1) = char(32); disp(x); end

サインインしてコメントする。

 採用された回答

KSSV
KSSV 2016 年 11 月 30 日
編集済み: KSSV 2016 年 11 月 30 日

1 投票

function drawBox(str,w,h)
ch = [0. 0.] ;
%%get four corners of box
N = 20 ;
x = linspace(ch(1),ch(1)+w,N) ;
y = linspace(ch(2),ch(2)+h,N) ;
[X,Y] = meshgrid(x,y) ;
X(2:end-1,2:end-1) = NaN ;
Y(2:end-1,2:end-1) = NaN ;
x = X(~isnan(X)) ;y = Y(~isnan(Y)) ;
plot(x,y,'.w') ; hold on
text(x,y,str)
axis off

4 件のコメント

Khalid Khan
Khalid Khan 2016 年 11 月 30 日
Sir my problem is still the same the function should ask the user about the character, height and width Plz help out THANK YOU for helping me
KSSV
KSSV 2016 年 11 月 30 日
Edited....it is easy to make a function from the script...you should have done it.
Khalid Khan
Khalid Khan 2016 年 11 月 30 日
編集済み: Khalid Khan 2016 年 11 月 30 日
Thank You Sir It worked
rim
rim 2016 年 12 月 2 日
編集済み: rim 2016 年 12 月 2 日
its not working for me , please help me with this question its showing error in functio drawBox

サインインしてコメントする。

その他の回答 (2 件)

KSSV
KSSV 2016 年 11 月 30 日

1 投票

what is ch? Actually to draw a box four numbers are needed. First two position/ origin of the box and then width, height. I think ch is a 1x2 array with two numbers which represent position of the box.
ch = [1. 1.] ;
h = 1. ;
w = 0.5 ;
box = [ch w h] ;
%%get four corners of box
p = [ch(1) ch(2) ;
ch(1)+w ch(2) ;
ch(1)+w ch(2)+h ;
ch(1) ch(2)+h ;
ch(1) ch(2)] ;
plot(p(:,1),p(:,2),'r')

3 件のコメント

Khalid Khan
Khalid Khan 2016 年 11 月 30 日
編集済み: Khalid Khan 2016 年 11 月 30 日
sir thank you that replied but my question is how to draw a box of characters. </matlabcentral/answers/uploaded_files/64381/Untitled.png>
Khalid Khan
Khalid Khan 2016 年 11 月 30 日
feg bog
feg bog 2016 年 11 月 30 日
編集済み: feg bog 2016 年 11 月 30 日
the given code do not give the wanted output is there is wrong on the code , or what !! i am confused i.e. is necessary to use plot or normal printf code?

サインインしてコメントする。

Abu Yamen
Abu Yamen 2016 年 12 月 3 日

0 投票

function []= drawBox('s',w,h)
ch =input('ch-ch ','s'); w =input('width : '); h =input('hight : ');
x = repmat(ch, h, w); x(2:end-1, 2:end-1) = char(32); disp(x); end

カテゴリ

ヘルプ センター および File ExchangeEntering Commands についてさらに検索

製品

質問済み:

2016 年 11 月 30 日

回答済み:

2016 年 12 月 3 日

Community Treasure Hunt

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

Start Hunting!

Translated by