Input data using GUI
조회 수: 14 (최근 30일)
이전 댓글 표시
Jordy Charly Isidore RABETANETIARIMANANA
2019년 3월 28일
댓글: Jordy Charly Isidore RABETANETIARIMANANA
2019년 3월 28일
I am working on a sensitivity analysis using EFAST method, i want to build an interface for this model. I want to ask the users to enter the number of frequency = n that they want, after that I want them to input the value of the n frequencies. Here is the program outside of GUI
n= input ('enter the number of frequency:');
for i=1:n
f(i)=input(['enter the frequency number', num2str(i),':']);
end
How to do it with GUI
댓글 수: 5
채택된 답변
Rik
2019년 3월 28일
Have a look at the code below, read the documentation for every function that you don't know and try to understand the flow of this program. Use the debugger to go through this code line by line.
function [n,values]=get_value_list
f=figure;
set(f,'Menu','none','Toolbar','none')%have a clean window
h=struct;
h.f=f;
%setup the GUI elements
h.editfield=uicontrol('Parent',f,...
'Style','edit',...
'HorizontalAlignment','left',...
'max',inf,...%maximum number of values
'Units','Normalized',...
'Position',[0.075 0.075 0.6 0.80]);
h.explanation=uicontrol('Parent',f,...
'Style','text',...
'String','Type the frequencies here, each values on one line:',...
'Units','Normalized',...
'Position',[0.075 0.875 0.6 0.075]);
h.testValuesButton=uicontrol('Parent',f,...
'Style','pushbutton',...
'String','Test if values are valid',...
'Callback',@testValues,...
'Units','Normalized',...
'Position',[0.725 0.5 0.225 0.375]);
h.outputValuesButton=uicontrol('Parent',f,...
'Style','pushbutton',...
'String','Output values',...
'Callback',@outputValues,...
'Units','Normalized',...
'Position',[0.725 0.075 0.225 0.375]);
guidata(h.f,h)%store handles to guidata to access them in callbacks
%wait for the 'output values' button
uiwait(h.f)
try
%reload h struct, which now contains the output values
h=guidata(h.f);
values=h.values;
%close figure
close(h.f)
catch
%the figure was closed during uiwait
values=[];
end
n=numel(values);
end
function testValues(hObject,evnt) %#ok<INUSD>
h=guidata(hObject);
str=get(h.editfield,'String');
if isempty(str)
h.values=[];
else
%convert to numeric values, and remove invalid values
vals=num2cell(str,2);
vals=cellfun(@str2double,vals);
vals(isnan(vals))=[];
%output the valid numbers back to the edit field
str=pad(cellfun(@num2str,num2cell(vals),'UniformOutput',false));
str=cell2mat(str);
set(h.editfield,'String',str);
%set output
h.values=vals;
end
guidata(h.f,h)
end
function outputValues(hObject,evnt) %#ok<INUSD>
h=guidata(hObject);
testValues(hObject)%will store the valid values in h.values
uiresume(h.f)
end
추가 답변 (1개)
Jan
2019년 3월 28일
편집: Jan
2019년 3월 28일
Do you want to create the GUI in AppDesigner, in GUIDE or programmatically? With the last one:
function Data = YourGUI
H.Fig = figure('Position', [100, 100, 300, 640]);
H.Edit = uicontrol(H.Fig, 'Style', 'edit', 'String', '', ...
'Position', [10, 610, 280, 25], ...
'Callback', @EditCB);
H.Ready = uicontrol(H.Fig, 'Style', 'PushButton', 'String', 'Ready', ...
'Position', [210, 10, 80, 25], ...
'Callback', @ReadyCB);
H.Table = uitable(H.Fig, 'Position', [10, 35, 280, 580], 'Visible', 'off');
guidata(H.Fig, H);
uiwait(H.Fig);
Data = H.Table.Data;
end
function EditCB(EditH, EventData)
H = guidata(EditH);
n = sscanf(EditH.String, '%d', 1);
set(H.Table, 'Data', cell(n, 1), 'Visible', 'on');
set(EditH, 'Enable', 'off');
end
function ReadyCB(ButtonH, EventData)
H = guidata(ButtonH);
uiresume(H.Fig);
end
UNTESTED CODE - debug this by your own.
댓글 수: 4
Rik
2019년 3월 28일
I meant your use of uitable instead of my uicontrol edit style. A table trades some space for more clarity that you need to enter values.
참고 항목
카테고리
Help Center 및 File Exchange에서 Structures에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!