Insert data in uitable from textbox

15 次查看(过去 30 天)
Abdullah Duraid
Abdullah Duraid 2019-3-23
回答: Hitesh 2024-8-30,5:19
Hi i’m working on program depends on inserting two numbers into two text boxes. When I press the pushbutton, the two numbers are combined and displayed in the first row of the table. When I enter two other numbers in the text boxes and press the pushbutton again, the new resolt is displayed in the second row of the table and etc. But the problem is that the new resolt replaces the old resolt in the first row What’s the code that allow me to set data in second row then thired etc....

回答(1 个)

Hitesh
Hitesh 2024-8-30,5:19
Hi Abdullah,
The challenge you are facing arises when the new results are added to the table without preserving the existing data, resulting in the current content being overwritten. To address this, kindly ensure that you retrieve the current data from the table before incorporating any new entries.
For guidance on updating the table while maintaining the existing data, please refer to the documentation and code provided below.
% txt1 is the textfield1, txt2 is textfiled2 and tb1 is table
function onButtonPushed(txt1, txt2, tbl)
% Get the numbers from text boxes
num1 = txt1.Value;
num2 = txt2.Value;
% Combine the numbers as a string
combinedStr = sprintf('%d%d', num1, num2);
% Get the current row index from UserData
currentRow = tbl.UserData;
% Get current data from the table
currentData = tbl.Data;
% Ensure the table has enough rows
if size(currentData, 1) < currentRow
currentData{currentRow, 1} = combinedStr;
else
currentData{end+1, 1} = combinedStr;
end
% Update the table data
tbl.Data = currentData;
% Increment the row index for the next entry
tbl.UserData = currentRow + 1;
end

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by