Insert data in uitable from textbox

조회 수: 3 (최근 30일)
Abdullah Duraid
Abdullah Duraid 2019년 3월 23일
답변: Hitesh 2024년 8월 30일
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일
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