# How to apply uitable to compute and display values those two purposes at the same time?

1 view (last 30 days)
Marco Poon on 30 Mar 2020
Commented: Marco Poon on 10 Apr 2020
I am a university student who recently study something related to computing and apply Matlab to solve some engineering problem. This time I am trying to using the GUI application which involve the uitable that I have never used before. I tried to made a GUI shown as below, all thing is going well and only one obstacle is the uitable. I had searched the stuff about the cell array and still no clue on using the uitable to display variable values or even the function will retrieve the data from the table to computate values for the next row. I think there is 2 ways to solve the problem:
1) Keep the current function and hide the variables until the computation is ended then display all the values into uitable. Its seem to be easy to handle on the complexity. or
2) Modified the code and make function will retrieve the data from the table to computate values for the next row and repeat in loops during its operation. Its sound to be messy for me as a mechanical eng. student, not a student from computer engineering or a smart guy on mathematics.
Anyway, I still unable to know how to coding for input my data into the uitable. Can someone give me a hand on provie some clear, practical solutions or some inspiration for me to solving this problem? Thx.
Here is the code of the function currently using in this GUI:
(This is a bisection algorithm that will fprint the values on the command window.)
function run_button_Callback(hObject, ~, handles)
% hObject handle to run_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
func = get(handles.func_input, 'String');
xL = str2double(get(handles.xL_input, 'String'));
xU = str2double(get(handles.xU_input, 'String'));
Es = str2double(get(handles.Es_input, 'String'));
imax = str2double(get(handles.imax_input, 'String'));
%% Formatting Output:
fprintf(' ---------------------------------------------------------------------------------------------\n')
fprintf(' It. xL xR xU f(xL) f(xR) f(xU) |%% Err|\n\n');
%% Initial Conditions:
iter = 0;
xR_P = NaN; % The First Old Estimate Of Root Is Unknown.
%% Computation:
F_xL = feval(func, xL);
F_xU = feval(func, xU);
% Condition 01:
if (F_xL * F_xU > 0)
T01
else
while (1) % If (F_xL * F_xU < 0)
xR = (xL + xU) / 2;
F_xR = feval(func, xR);
% Compute Perc. Relative Appr. Error:
if (xR ~= 0)
Ea = abs((xR - xR_P) / xR) * 100;
end
% Output:
fprintf(' %3.0f %12.6f %12.6f %12.6f %11.6f %11.6f %11.6f %10.6f %%\n', iter, xL, xR, xU, F_xL, F_xR, F_xU, Ea);
fprintf(' >---\n\n')
% Condition 02:
Test = F_xL * F_xR;
if (Test < 0)
xU = xR;
F_xU = F_xR;
elseif (Test > 0)
xL = xR;
F_xL = F_xR;
else
Ea = 0;
end
% Increment Settings:
xR_P = xR; % Update The Old Estimate Of The Root
iter = iter + 1;
% Condition 03:
if (Ea < Es) %(Case: 1)
set(handles.status_output, 'String', 'Tolerance Is Satisfied.');
break
end
% Condition 04:
if (iter > imax) %(Case: 2)
set(handles.status_output, 'String', 'Iteration Limit Exceeded.');
break
end
end
fprintf(' ---------------------------------------------------------------------------------------------\n')
handles.xR_output.String = sprintf('%.6f', xR);
set(handles.iter_output, 'String', iter);
end
% Update handles structure
guidata(hObject, handles);
Note: T01 is a m.file which will popup a GUI window to show the error message if f(xl)*f(xu) > 0, it works properly. Peng Li on 30 Mar 2020
Sorry didn't quite get your problem. are you trying to update the uitable as program is runing?
If it involves a loop in your callback, you could do something like
% show table
if app.ind_out == 1 % refresh, app.ind_out indexing your loop
app.ResTable.Data = [];
end
app.ResTable.Data{app.ind_out, 1} = app.yourDataHandle;
app.ind_out = app.ind_out + 1;
Marco Poon on 10 Apr 2020
Recently, I have try to change my code from screen print table to the code for uitable. Yeah, It works. But the value shown is wrong.
Here is the code i have changed:
function run_button_Callback(hObject, eventdata, handles)
% hObject handle to run_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
func = get(handles.func_input, 'String');
f = str2func(func);
xL = str2double(get(handles.xL_input, 'String'));
xU = str2double(get(handles.xU_input, 'String'));
Es = str2double(get(handles.Es_input, 'String'));
imax = str2double(get(handles.imax_input, 'String'));
%% Initial Conditions:
iter = 0;
xR_P = 0; % The First Old Estimate Of Root Is Unknown.
%% Computation:
F_xL = f(xL);
F_xU = f(xU);
% Condition 01:
if (F_xL * F_xU > 0)
T01
else
xR = (xL + xU) / 2;
F_xR = f(xR);
i = 1;
Ea = 100;
while (1)
%save data
data(i, 1) = iter;
data(i, 2) = xL;
data(i, 3) = xR;
data(i, 4) = xU;
data(i, 5) = F_xL;
data(i, 6) = F_xR;
data(i, 7) = F_xU;
% Condition 02:
Test = F_xL * F_xR;
if (Test < 0)
xU = xR;
F_xU = F_xR;
elseif (Test > 0)
xL = xR;
F_xL = F_xR;
end
% Compute Perc. Relative Appr. Error:
if (i > 1)
Ea = abs((xR - xR_P) / xR) * 100;
end
data(i, 8) = Ea;
% Condition 03:
if (Ea < Es) %(Case: 1)
set(handles.status_output, 'String', 'Tolerance Is Satisfied.');
break;
end
% Condition 04:
if (iter > imax) %(Case: 2)
set(handles.status_output, 'String', 'Iteration Limit Exceeded.');
break;
end
% Increment Settings:
xR_P = xR; % Update The Old Estimate Of The Root
xR = (xL + xU) / 2;
iter = iter + 1;
i = i + 1;
end
end
set(handles.uitable1, 'Data', data);
handles.xR_output.String = sprintf('%.6f', xR);
set(handles.iter_output, 'String', iter);
Here is the new code output: But the value is wrong in somewhere...
here is the screen print table: R2018a

### Community Treasure Hunt

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

Start Hunting!