フィルターのクリア

Insert random binary into table

1 回表示 (過去 30 日間)
Ibnu Darajat
Ibnu Darajat 2019 年 5 月 11 日
コメント済み: Walter Roberson 2019 年 5 月 11 日
I want to add binary numbers randomly to table and the binary size is obtained from edit text. and this is my code
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global p
n = str2double(get(handles.edit1, 'String'));
x = randi([0 1],n,1);
p.MyData = [p.MyData; {x}];
set(handles.uitable1, 'Data',p.MyData);
This script works when I type 1 in edit text, I can enter binary numbers one by one. when I type > 1, code error. Error message is :
Error using matlab.ui.control.Table/set
While setting the 'Data' property of 'Table':
Data within a cell array must have size [1 1]
Error in untitled3>pushbutton1_Callback (line 82)
set(handles.uitable1, 'Data',p.MyData);
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in untitled3 (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)untitled3('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback
I want to insert binary numbers according to the numbers I write in edit text. Please help me

採用された回答

Walter Roberson
Walter Roberson 2019 年 5 月 11 日
p.MyData = [p.MyData; num2cell(x)];
  4 件のコメント
Ibnu Darajat
Ibnu Darajat 2019 年 5 月 11 日
編集済み: Ibnu Darajat 2019 年 5 月 11 日
I mean, if I enter a number to the edit text then the result of the binary is in the row.
like this:
n = 3. the result is 3x1 cell array
1
0
1
not 3x3 cell array
1 0 1
0 1 1
1 0 1
If I use num2cell (x (:)) the binary total is 9 not 3
please help me
Walter Roberson
Walter Roberson 2019 年 5 月 11 日
You defined
n = str2double(get(handles.edit1, 'String'));
x = randi([0 1],n,1);
so your n, which is not documented for us, appears to be intended to control the number of entries to be generated, rather than the number of bits per entry.
How many bits are there intended to be per entry?
Is the intention that the number of bits per entry should be the same as the number of entries?

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeString Parsing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by