generate random binary number inside a textbox

hello
i was trying to generate a random binary sequence like 110010(when Clicking a button) and place it inside an edit text in GUI. but what i am getting instead is this 1
1
0
0
i am getting the numbers in the edit text vertically . how can i generate them in a way , 1 row and n column
this is my code
n=10;
x = randi([0,1],1,n);
edit= findobj(0, 'tag', 'try2');
set(edit, 'string', x);
note that edit is the name of edit text. i would appreciate any help .thanks in advance

 採用された回答

Image Analyst
Image Analyst 2021 年 8 月 1 日

0 投票

Try this:
n=10
x = randi([0,1], 1, n)
strX = sprintf('%d ', x) % Can remove the space after %d if you want.
handles.try2.String = strX; % If using GUIDE
% Or app.try2.String if you're using App Designer.

1 件のコメント

anees rafeh
anees rafeh 2021 年 8 月 1 日
Thank you so much it worked.

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

その他の回答 (1 件)

Yongjian Feng
Yongjian Feng 2021 年 8 月 1 日
編集済み: Yongjian Feng 2021 年 8 月 1 日

0 投票

Try this:
n = 0;
x = randi([0,1],1,n);
y = num2str(x);
y = strrep(y, ' ', '') % remove the space between digits if you want
edit= findobj(0, 'tag', 'try2');
set(edit, 'string', y);

3 件のコメント

anees rafeh
anees rafeh 2021 年 8 月 1 日
Now it is compiling nothing(blank)
Yongjian Feng
Yongjian Feng 2021 年 8 月 1 日
You need to put all the other information back. Modified code above.
Try to debug the code to see where it broke, if it still doesn't work. Put a break point to make sure x is good, y is good, and edit is not empty.
anees rafeh
anees rafeh 2021 年 8 月 1 日
Thank you for your time!

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

カテゴリ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by