GUI design for plotting boxplot - matlab

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

Related

How to use multiple user inputs in a function in MATLAB?

I have difficulty in writing an algortihm that takes three numbers as input from the user and calculates the maximum of them. I'm trying to do these by using function, however, I get an error message: "Undefined function 'calc' for input arguments of type 'char'.
Error in Untitled (line 2) calc(numbers); "
Here's my code: (I'm new at coding so there may be other types of errors:))
numbers= input('Enter three numbers to find out maximum of them:','s');
calc(numbers);
maxi
function [ maxi ] = calc( numbers(1),numbers(2),numbers(3) )
%UNTÄ°TLED2 Summary of this function goes here
% Detailed explanation goes here
maxi= numbers(1);
if numbers(2)>maxi
maxi= numbers(2)
end
if numbers(3)>maxi
maxi= numbers(3)
end
end
As you say that "I'm new at coding", I thought I'd describe a couple of different approaches for this.
Reading the input
You could do as H.Ghassami suggests and read the input one by one.
This is probably the better option as there is some fault handling
built into it. The user can only enter one input at a time and there
is check that the inputs is evaluable (a number or a variable from
the current workspace). The rutin could get a little bit more general by adding a variable for the number of inputs to get.
numberOfInputs = 3;
number = zeros(1, numberOfInputs);
for idx = 1:numberOfInputs
% Get the number of inputs declared in numberOfInputs
number(idx) = input( sprintf('Enter number %d: ', idx));
end
You could also let the user enter all the numbers at once by, as the example in your question, add a 2nd argument 's' to input. The input is now treated as a string. The user has to separate the input numbers in some way, preferable with a whitespace. You then have to convert to string to a number vector.
numberOfInputs = 3;
number = input( sprintf('Enter %d numbers separated by whitespaces\n', numberOfInputs), 's');
number = str2num(number); % Convert to number array
You should probably do some check on the number array to see that it is valid (numbers of the correct amount)
Getting the max
Matlab has an buildin function for this. So you could just write
maxNumber= max(number);
or if you, perhaps for the exercise, would like to use an if structure you could make it more general with a for loop
maxNumber = number(1);
for idx = 2:numberOfInputs
if maxNumber < number(idx)
maxNumber = number(idx);
end
end
-
The entire solution could be encapsulated in a function
function maxNumber = getMaxInput(numberOfInputs)
Your first mistake is input . It just get just one string from user, but you need three numbers.
Second one is the next line in your script. When you calculate the numbers, you should send output to a variable. Also you should change the calc parameters.
Put this code in your script instead of your code :
number1= input('Enter number1:');
number2= input('Enter number2:');
number3= input('Enter number3:');
maxi=calc(number1,number2,number3)
%---------------------------------------------------
function [ maxi ] = calc( numbers1,numbers2,numbers3 )
%UNTÄ°TLED2 Summary of this function goes here
% Detailed explanation goes here
maxi= numbers1;
if numbers2>maxi
maxi= numbers2;
end
if numbers3>maxi
maxi= numbers3;
end
end
refrence to read for you: http://matlabgeeks.com/tips-tutorials/getting-user-input/

create an edittext and get its data in another gui matlab

I want to get a number in a gui (say N), and create N "edittext" in another gui figure when pushing a "pushbutton". I write this code to do so (which works correctly):
%%%%%%%%%%%%%%%%%%%%%%%%%%%
input_num = str2double(get(handles.edit1,'String'));
edittext = zeros(input_num,1);
panel= uipanel('parent',untitled1,...
'Title','Input Data',...
'position',[.01 .05 .25 .95]);
for i = 1:input_num
edittext(i,1) = uicontrol('parent',panel,'style','edit',...
'string','',...
'position',[20 360-i*25 40 20]);
end
%%%%%%%%%%%%%%%%%%%%%%
then in the second gui, I want to get the data which are entered by user in the created "edittext"s and use them to do something.
How can I do that?
You can do it using the Tag properties of the figures and uipanel you are creating to fetch the appropriate element from the 1st GUI in the 2nd GUI.
Here is a commented example:
Let's create GUI1 with the original tag 'GUI1'. Notice that we also add a tag to the uipanel...and that we use k as the loop variable instead of i.
function MakeEditTextsGUI
hFig1 = figure('Name','Fig1','Tag','GUI1');
input_num = 3;
edittext = zeros(input_num,1);
panel= uipanel('parent',hFig1,'Tag','Panel1',...
'Title','Input Data',...
'position',[.01 .05 .25 .95]);
for k = 1:input_num
edittext(k,1) = uicontrol('parent',panel,'style','edit',...
'string','','position',[20 360-k*25 40 20],'Tag',sprintf('edit_%i',k));
end
end
This is the figure in which the user enters the data.
Let's create a 2nd GUI with a pushbutton that the user pushes to fetch the data from the 1st GUI, which must be opened:
function GetEditBoxes
hFig2 = figure('Position',[400 400 200 100],'Tag','GUI2');
hButton = uicontrol('Style','push','Position',[60 60 80 20],'String','Get content','Callback',#(s,e) GetContent);
function GetContent
%// Fetch figure 1
hCurrFig = findobj('Tag','GUI1');
%// Make current figure
figure(hCurrFig);
%// Fetch Panel1
hCurrPanel = findobj('Tag','Panel1');
%// Get its children and order from first to last
EditBoxes = flipud(get(hCurrPanel,'Children'));
%// Get number of edit boxes
NumBoxes = numel(EditBoxes);
%// Go and get the data!
for p = 1:NumBoxes
get(EditBoxes(p),'String')
end
end
end
I did not include any error catching code but its easy to verify if every edit box contains an entry. Once the user presses the button, the content of each box is displayed in the command window but you can easily store them into an array or something.
Have fun!

Customizing cursor in Matlab

In Matlab 2012a I have generated a figure from a previous code that is SSI as a function of age.
I want to customize datatip by updating my own function instead of the default one. I know how to change x and y and now I have Age and SSI for them. However, I have another piece of information -subjectID- which I want to add to the display text.
By clicking on each point, I want datatip to show Age, SSI and subject ID of corresponding data point.
This is what I have now:
matlab is a saved work place of my SSI-age.
function output_txt = myupdatefcn(obj,event_obj,...
matlab,labels,SubjectID)
pos = get(event_obj,'Position');
x = pos(1);
y = pos(2);
[~, ~, raw0_0] = xlsread('Data.xlsx','CONTROLS','A2:A106');
raw = [raw0_0];
SubjectID = cell2mat(raw);
output_txt = {['AGE: ',num2str(pos(1),4)],...
['SSI: ',num2str(pos(2),4)],...
['SubjectID: ',SubjectID]};
idx = find(matlab == x,1);
[row,col] = ind2sub(size(matlab),idx);
output_txt{end+1} = cell2mat(labels(row));
Obviously, this is not right. Can somebody please help me out here? Thank you.
If I read your code correctly, I make the following assumptions (which may be incorrect):
* the subjectID is a cell containing a vector of strings
* subjectID is X position of the clicked point
First, a quick digression: getting SubjectID into your plot
I noticed, in your function call, you have SubjectID as one of the input parameters. However, it appears that it would never be used, since the next line that uses it assigns it a value.
As written, this will read from the excel file each and every time that the update function is called. You might wish to move the load-from-excel portion into the same section of code where the data is first loaded. If I assume the SubjectID is text, you can store it in the UserData variable of the timeseries. That would make the following work:
Onward to the answer
So, if you include your SubjectID information in the userdata, when you first plot like so:
% ...not shown: get the ages, SSIs and SubjectIDs ....
plot(ages, SSIs, 'UserData', SubjectIDs); % Store SubjectIDs along with the line...
Then the following should work -- or at least put you on solid ground.
function output_txt = myupdatefcn(obj,event_obj)
pos = get(event_obj,'Position');
x = pos(1);
y = pos(2);
allIDs = get(event_obj.Target,'UserData');
thisSubject = event_obj.UserData{pos(1)};
output_txt = {['AGE: ',num2str(pos(1),4)],...
['SSI: ',num2str(pos(2),4)],...
['SubjectID: ',thisSubject]};
You can probably get rid of the last 3 lines of code, since you know a priori that all 3 values are accessible.
Hope that helps.

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

how to prevent input dialog box be closed in 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