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 件のコメント

abouessire ismail
abouessire ismail 2019 年 3 月 28 日
Hi .
i hope that my answer can solve your problem .
firstly create a gui that contain an edit text to get the number of frequency that are stored in a variable by the next instruction :
temp1=str2double(get(hObject,'String'));
assignin('base','n',temp1); %get number from the gui to workspace.
% if you set in edit text a caracter you should give you a messageBox.
if isnan(temp1)
errordlg('set please a number not a caracter !!!','File Error');
end
secondly,we create in the same gui a button that contain in button_callback :
n= evalin('base','n'); %from worksapce to gui;
for i=1:n
f(i)=input(['enter the frequency number', num2str(i),':']);
end
that's it.
Have a nice day.
Rik
Rik 2019 年 3 月 28 日
Do not use evalin. Write a function that opens a modal box instead. Look into the uicontrol, uiwait and guidata functions. I'll write up a function for you in a while.
Adam
Adam 2019 年 3 月 28 日
Using input in a GUI doesn't make sense - that is for keyboard input. If you have a GUI then have GUI components ask the user for any inputs. I'll assume Rik's solution will cover things though so I won't add any more!
Thank you for your answer but the for loop didn't work. My problem is how to input many arguments with Gui
Thank you in advance Rik and Adam!

サインインしてコメントする。

 採用された回答

Rik
Rik 2019 年 3 月 28 日

1 投票

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 件のコメント

This solves my problem!!! Thank u!!!

サインインしてコメントする。

その他の回答 (1 件)

Jan
Jan 2019 年 3 月 28 日
編集済み: Jan 2019 年 3 月 28 日

2 投票

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
Rik 2019 年 3 月 28 日
A table might be a better idea than a plain edit field. For the rest it is funny to see how much we agree on the approach.
Jan
Jan 2019 年 3 月 28 日
編集済み: Jan 2019 年 3 月 28 日
@Rik: Yes, the similarities are funny.
Do you mean a table with a single cell just to fill in the number of rows?
Rik
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.
Jan
Jan 2019 年 3 月 28 日
編集済み: Jan 2019 年 3 月 28 日
@Rik: Now I got it. The uitable has the disadvantage, that the number of rows must be known in advance.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeStructures についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by