GUI 'uitable' with option to add or remove rows depending on 'textbox' value

4 ビュー (過去 30 日間)
David (degtusmc)
David (degtusmc) 2013 年 7 月 11 日
Hello everyone,
I am trying to create a 2 by 3 table (initially) in a GUI using GUIDE, with a textbox that controls the number of rows of the table. The columns will stay constant.
I read through the documentation and also multiple threads on the subject, but I am still not understanding how to accomplish this. Is what I am trying to do possible?
From what I understand, I have to change the 'data' portion of the uitable properties since the "number of rows in the table is the larger of RowName and the number of rows in Data". Any ideas would be greatly appreciated.
Thank you in advance for any help provided.

採用された回答

Evan
Evan 2013 年 7 月 11 日
編集済み: Evan 2013 年 7 月 11 日
It looks like you just need to put the following i the callback of your editbox (I'm assuming you meant editbox instead of textbox):
function myEdit_Callback(hObject,eventdata,handles)
nRows = str2num(get(hObject,'String'));
dat = cell(nRows,3);
set(handles.myTable,'Data',dat)
guidata(hObject,handles)
That, of course, will overwrite any data entered in your table with a blank table. If you want to keep your data, it'll be a bit more complicated:
function myEdit_Callback(hObject,eventdata,handles)
oldDat = get(handles.myTable,'Data');
nRows = str2num(get(hObject,'String'));
if nRows > size(oldDat,1)
dat = cell(nRows,3);
dat(1:nRows,:) = oldDat;
elseif nRows < size(oldDat,1)
dat = oldDat(1:nRows,:);
end
set(handles.myTable,'Data',dat)
guidata(hObject,handles)
  1 件のコメント
David (degtusmc)
David (degtusmc) 2013 年 7 月 11 日
編集済み: David (degtusmc) 2013 年 7 月 12 日
Thank you so much! That did exactly what I needed.
The second part did not work so well, but I will try to get it working. I tried the following:
oldData = get(handles.table_uitable,'Data')
nRows = str2num(get(hObject,'String'))
testSize = size(oldData,1)
if nRows > size(oldData,1)
dat = cell(nRows,3)
dat(1:testSize,:) = oldData
elseif nRows < size(oldData,1)
dat = oldData(1:nRows,:)
end
set(handles.table_uitable,'Data',dat)
guidata(hObject,handles)
but, there is a problem on the sixth line when assigning the old values. First I had to make a minor correction by changing 1:nRows to 1:testSize. Since the problem is the conversion from double to cell, I tried using num2cell() which semi fixed the problem; but as you keep increasing the rows it gives an error when you try to set the values within the cell.
Any suggestions?
Once again, thank you for your help.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by