How to get output multi from GUI ?
1 view (last 30 days)
Show older comments
Hello
following code. I want to get repeatable output from GUI for example
A = 2
B= 3
ans = 6
I want to type input again like
A = 5
B= 4
ans = 20
I want to get 6 and 20 out of Testcal2 function and save somewhere like workspace .
then i want ans array like this
ans_array = [6 20]
finally
i want to sum all variable in ans_array
sum(ans_array,'all')
Could you help me please ?
function Testcal2
fig = uifigure('Name','program');
fig.Position = [100 100 800 500];
A = uieditfield(fig,'Position',[20 300 100 22],'ValueChangedFcn',@multiply);
B = uieditfield(fig,'Position',[180 300 100 22],'ValueChangedFcn',@multiply);
anslabel = uilabel(fig,'Position',[500 300 100 15]);
btn = uibutton(fig,'push','Position',[400, 300, 60, 22],'ButtonPushedFcn', @showans);
function multiply(src, event)
global ans
var1 = str2double(A.Value)
var2 = str2double(B.Value)
ans = var1*var2;
end
function showans(src, event)
global ans
anslabel.Text = num2str(ans);
end
end
2 Comments
Geoff Hayes
on 1 Apr 2020
Thapakorn - why do you need to write the output to the workspace? Can't you just store the data to local variables (arrays) in your Testcal2 function and then write/save it to a mat file?
Answers (1)
Geoff Hayes
on 1 Apr 2020
Thapakorn - try the following code
function Testcal2
fig = uifigure('Name','program');
fig.Position = [100 100 800 500];
A = uieditfield(fig,'Position',[20 300 100 22],'ValueChangedFcn',@multiply);
B = uieditfield(fig,'Position',[180 300 100 22],'ValueChangedFcn',@multiply);
anslabel = uilabel(fig,'Position',[500 300 100 15]);
btn = uibutton(fig,'push','Position',[400, 300, 60, 22],'ButtonPushedFcn', @showans);
ans_array = []; % <---- nested function have access to this variable
function multiply(src, event)
var1 = str2double(A.Value)
var2 = str2double(B.Value)
ans_array(end+1) = var1*var2;
end
function showans(src, event)
anslabel.Text = num2str(ans_array(end));
save('Testcal2.mat', ans_array); % <----- save the answers to a mat file
end
end
Note how the ans_array is updated whenever multiply is called, and we save the results to a mat file whenever showans is called.
0 Comments
See Also
Categories
Find more on Variables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!