how to prevent input dialog box be closed in matlab? - matlab

how can the input dialog box can be avoided from directly close by the user without enter any value? for the 'menu' function we can form loop using while options==0 for unselected options but how about input dialog?
prompt = {'Enter gain:','Enter range:'};
dlg_title = 'Enter values';
num_lines= 1;
def = {'20','256'}; %default
answer = inputdlg(prompt,dlg_title,num_lines,def);
%%%to get the two entered values%%%%
%A = getfield(answer,{1}); %first input field
A = str2double(answer{1});
%B = getfield(answer,{2}); %second input field
B = str2double(answer{2});
suppose I have a input dialog as shown, how I can model it with the loop in complete way

You cannot prevent it from being closed, but you can use a while-loop to re-open it until the user has entered a useful value.
done = false;
while ~done
a=inputdlg('enter a number')
num = str2double(a{1}); %# will turn empty and strings into NaN
if isnumeric(num)
done = true;
else
%# keep running while loop
%# you can pop up an errordlg box here to tell the user what was wrong.
%# It would be nice if you were to set the inputs that passed the test
%# as defaults for the next call of inputdlg. Nothing sucks as much
%# as having to completely re-fill a form because of a small typo
end
end

Related

GUI design for plotting boxplot

let us suppose that we have following GUI
idea is following: user enters the input or number of variables, and then in cycle he will enter step by step that amount of variables, for instance let us suppose that number of variables is 5
so user enter 1, then clicks enter and cell will be empty, then enters 2 , then enter and so on and result will be saves in pre-allocated array,here is code
n=str2num(get(handles.observation_number,'String'));
array=zeros(1,n);
for ii=1:n
array(ii)=str2num(get(handles.variables,'String'));
end
axes(handles.axes1);
boxplot(array);
but program does not gives me possibility to do that, how can i fix it? thanks in advance
You have to create a callback function, something like:
enterVar=uicontrol('Style', 'edit','Callback',{#addVar});
Where addVar is a function that assign the current value to the last empty position of "array" variable and erase the content of the text box.
I would use the inputdlg() in your place:
for ii=1:n
prompt = {'Enter a variable:'};
dlg_title = 'Input';
num_lines = 1;
answer = inputdlg(prompt,dlg_title,num_lines);
array(ii) = str2double(answer);
end

MATLAB GUI User Input Update

I have a MATLAB GUI which takes values from the user input , do some calculation based on those and plot them in the GUI.
Everything is all right and working BUT when I change the values in the GUI it does not plot as expected and gives and error of dimension mismatch. However, there is no dimension mismatch and if I go to the script and press run again (the big green arrow) and then click plot again, it plots the values as expected.
I have initialized all of my values that I use but I can not see how can fix this. I need something like update command for all of the script each time I press the 'plot' button.
Any help is appreciated, many thanks!..
CODE:
a = str2double(get(handles.edit14,'String'));
b = str2double(get(handles.edit15,'String'));
c = str2double(get(handles.edit16,'String'));
d = str2double(get(handles.edit18,'String'));
e = str2double(get(handles.edit19,'String'));
f = str2double(get(handles.edit20,'String'));
for Veff=[a:b:c] %user input speeds (a,b,c) (comes from user)
q = 0.5*1.225*(Veff*1000/3600)^2;
F1=q*S; M1=q*S*Cref;
FX1=(F1*coef(:,1)); FZ1=(F1*coef(:,3)); MM1=(M1*coef(:,5));
W=[d*g:e*g:f*g]; %define mass range (d,e,f comes from user)
for lw=1:1:length(W)
XGEquation2max1 = ((((-FB*(Xa-Xb))-(((FX1(12)))*(Za-Zr))-(((FZ1(7))))*(Xa-Xr))-((max(MM1))))./W(lw)) + Xa;
CGPercent(lw,column) = (XGEquation2max1 - Cstart)/Cref * 100;
end
column=column+1;
end
speed_matrix = [a:b:c];
mass_matrix = [d:e:f];
ns = size(speed_matrix);
ns= ns(2);
count5 = 1;
for ns=1:1:ns
hold all
axes(handles.axes4)
plot(CGPercent(:,ns),transpose(mass_matrix)/1000)
count5 = count5 + 1;
end
Main problem is with the variables 'd,e,f' because when I change 'a,b,c' only (i.e speed) there is no problem.

Save multiple GUIdata sets to listbox and then load them again in MATLAB

I have built a calculation software in MATLAB GUIDE. What I want to do is to fill out all my calculation data in different edit fields and some dropdowns and when I press calculate a "listbox" should be populated with the text "Calculation 1".
If I then change some data in some of the input fields and press calculate again I want to populate the listbox with "Calculation 2" beneath "Calculation 1" etc...
But then I would want to be able to highligt "calculation 1" again in the listbox and press a "load input parameters" button to populate all the edit input fields with the data that was used when "calculation 1" was calculated.
I have looked all over the place for this but can't find anything.
//Robin
Here is some code which is very basic but performs what you are looking for. There are a lot of tweaks possible but I'll let you play around with them. I put explanations as comments. You can copy past into Matlab and change the GUI as you like.
function CalculatorGUI
% Dummy GUI to calculate A*B + C...
clc
clear
close all
global hTestResult hEditA hEditB hEditC CalculationList CalculationStrings
% Set up controls
CalculationList = nan(10,3); % Create array in which we store the parameters. 1st column is A, 2nd is B and 3rd is C.
CalculationStrings = cell(10,1);
ScreenSize = get(0,'ScreenSize');
hFig = figure('Visible','off','Position',[ScreenSize(3)/2,ScreenSize(4)/2,450,285]);
hCalculateButton = uicontrol('Style','Pushbutton','Position',[350,150,80,30],'String','Calculate!','Callback',#CalculateCallback);
hTitle = uicontrol('Style','Text','Position',[100,250,100,25],'String','Calculate (A * B) + C');
hTextA = uicontrol('Style','Text','Position',[125,220,70,25],'String','A');
hEditA = uicontrol('Style','Edit','Position',[125,200,70,25],'String','1');
hTextB = uicontrol('Style','Text','Position',[200,220,70,25],'String','B');
hEditB = uicontrol('Style','Edit','Position',[200,200,70,25],'String','2');
hTextC = uicontrol('Style','Text','Position',[275,220,70,25],'String','C');
hEditC = uicontrol('Style','Edit','Position',[275,200,70,25],'String','3');
hResultHeader = uicontrol('Style','Text','Position',[350,220,70,25],'String','Result');
hTestResult = uicontrol('Style','Text','Position',[350,200,70,25],'String','');
hTextCalcu = uicontrol('Style','Text','Position',[100,140,100,50],'String','Calculations');
hListCalcu = uicontrol('Style','Listbox','String','','Position',[100,120,200,50],'max',10,...
'min',1,'Callback',#ListBox_Callback);
set(hFig,'Visible','on')
%======================================================================
%======================================================================
% Callback of the pushbutton
function CalculateCallback(~,~)
% Get the values in the edit boxes. There is no ckechup to make
% sure the user entered a correct value...
A = str2double(get(hEditA,'String'));
B = str2double(get(hEditB,'String'));
C = str2double(get(hEditC,'String'));
Calculation = A*B+C;
% Display the result.
set(hTestResult,'String',sprintf('The result is %0.2f',Calculation));
% Find how many calculations have been performed and append the
% parameters to the current list
[x,~] = find(~isnan(CalculationList));
CurrentCalc = numel(unique(x)); % Get number of rows which are NOT NaNs.
CurrentValues = [A B C];
CalculationList(CurrentCalc+1,:) = CurrentValues;
CurrentString = sprintf('A = %0.2f B = %0.2f C = %0.2f',A,B,C);
% Assign the parameters to the cell array.
CalculationStrings(CurrentCalc+1) = {CurrentString};
set(hListCalcu,'String',CalculationStrings)
end
% Listbox callback: When the selection changes, the corresponding
% parameters in the edit boxes change.
function ListBox_Callback(~,~)
SelectedCalc = get(hListCalcu,'Value');
CalculationList(SelectedCalc,1)
CalculationList(SelectedCalc,2)
CalculationList(SelectedCalc,3)
set(hEditA,'String',CalculationList(SelectedCalc,1));
set(hEditB,'String',CalculationList(SelectedCalc,2));
set(hEditC,'String',CalculationList(SelectedCalc,3));
end
end
The actual interface looks like this:
Of course you can make it much more complex, but this should help you get started and understand how the different callbacks work together. Have fun!

Need help on clarification of input dialog of matlab

Suppose I have the following code:
%%input dialog box%%%
prompt = {'Enter gain:','Enter range:'};
dlg_title = 'Enter values';
num_lines= 1;
def = {'20','256'}; %default
answer = inputdlg(prompt,dlg_title,num_lines,def);
%%%to get the two entered values%%%%
A = getfield(answer,{1}); %first input field
A = str2double(A);
B = getfield(answer,{2}); %second input field
B = str2double(B);
What does it mean to use "dynamic field names with structures instead of getfield"?
How can I use a loop for the input values that are complex and smaller than zero, in the sense to request the user for another compatible input?
I've tried the following loop but it does not work. Why?
while isnan(A) || ~isreal(A) || A<0
prompt = {'Enter gain:'%'Enter range:'};
dlg_title = {'undefine!!'};
num_lines= 1;
def = {'',''}%{'20','256'}; %default
answer = inputdlg(prompt, dlg_title, num_lines, def);
A = getfield(answer,{1}); %first input field
A = str2double(A);
%A = str2double(input('Enter the value of module(mm) : ', 's'));
end
The calls to getfield aren't required and frankly don't make any sense. The variable answer is a cell array not a struct so you don't need to use getfield The second half of the pasted code could be replaced with:
A = str2double( answer{1} );
B = str2dobule( answer{2} );
With regards to looping until you get valid input. You can use a boolean flag, a while, loop and a function (lets call it areInputsValid()) that returns true if the inputs are valid
validInput = false;
while (~validInput)
% input dialog box
% code here
% get the two entered values
& code here
validInput = areInputsValid(A, B);
end

How to create a pop up remainder message on input dialog in Matlab?

How can I create a reminder message as shown in the figure for instance the matrix size enter must greater than 30 and until the input is satisfy this condition ONLY the OK button on the dialog box can be pressed. In other words, only satisfy the condition only the input can be read into the program.
thank you everyone for the helps.
Source code for the dialog above:
prompt = {'Enter matrix size:','Enter colormap name:'};
dlg_title = 'Input for peaks function';
num_lines = 1;
def = {'20','hsv'};
answer = inputdlg(prompt,dlg_title,num_lines,def);
For the reminder, just set a tooltip property...
For Ok button, check that input satisfies required conditions in the callback function and only accept the dialog if it does