Editing fields in matlab - matlab

I am creating message box in MATLAB with the following code
prompt={'Length'}
name = 'Input';
answer = inputdlg(prompt,name,[1 40],defaultans);
Length = str2double(answer{1});
choice = questdlg('Would you like to confirm?', ...
'Message Box', ...
'Yes','No','No');
switch choice
case 'Yes'
h = msgbox({'Operation' 'Completed'});
case 'No'
h = msgbox({'Operation' 'Failed'});
end
I am entering the value as shown in below image
After moving to next window, when i press 'No' , I want the same earlier Input window as shown above to be displayed with 120 written inside the input box so that i can change the value.
Can anyone please let me know how to switch to previous window wherein i can edit my values which have been earlier written.

Use an infinite while loop and put the inputdlg statement inside it. When the user confirms, break it.
Modified Code:
prompt={'Length'};
name = 'Input';
defaultans={'120'};
while 1
answer = inputdlg(prompt,name,[1 40],defaultans);
choice = questdlg('Would you like to confirm?', ...
'Message Box', ...
'Yes','No','No');
switch choice
case 'Yes'
h = msgbox({'Operation' 'Completed'});
Length = str2double(answer{1});
break;
end
end

Related

Having problems with questdlg in matlab

I'm sorry for having only a part of the code but it is unnecessarily complicated. I want to have multiple question dialog boxes embedded with cases. The first switch case which is switch secim works fine when I put the last option twice which is 'Uc','Uc' and unless I write it that way it shows only the other options. But when I do the same thing for the inner switch case which isswitch secim2 it gives an error on the line secim2 = questdlg('İslem?', ... and when I delete the second 'Birim vektor' it works fine but doesn't show the Birim vektor option at all. How do I fix this?
secim = questdlg('Vektorler kac boyutlu?', ...
'Vektor', ...
'Bir','İki','Uc','Uc');
switch secim
case 'Bir'
secim2 = questdlg('İslem?', ...
'Vektor', ...,
'Toplam','Fark','Skaler Carpim','Birim vektor');
switch secim2
...
If you see the documentation, the syntax you're using in secim when you're repeating 'Uc' is:
button = questdlg(qstring,title,str1,str2,str3,default)
As mentioned in the documentation, default should be one of str1, str2 and str3 and hence when you repeat 'Uc', it takes 'Uc' as the default value and you have no problem.
In secim2, 'Birim vektor' doesn't match any of the three strings and hence you'd get this warning:
Warning: Default character vector does not match any button
character vector name.
If you repeat 'Birim vektor', it doesn't match with any of the valid syntaxes.
Above explanation is also pointed out in the comments.
For more than three strings, use listdlg.
choices = {'Toplam' ,'Fark','Skaler Carpim','Birim vektor'};
SelInd = listdlg('Name','Vektor', 'PromptString','İslem?','ListString',choices,...
'CancelString', 'Default Choice', 'SelectionMode','single',...
'ListSize',[200 100]) %adjust listsize as per requirement
SelInd(end+1) = 4; %Default Choice (Biriam vektor)
secim2 = choices{SelInd(1)};
which gives:

MATLAB programmatic GUI listbox value and data it refers to

I am making a GUI programmatically and have run into a small question regarding the uicontrol listbox and the data each 'value' or 'string' refers to. I feel this question will best be illustrated with some code. The code at the end of this question illustrates my question.
If you run this example, select all 5 files from the left listbox and press 'Button1', you will see them go into the right listbox. You can then select 1, or several of the files in the right list box and press 'Button2' and MATLAB will output the correct files names. All is fine here.
If you close and re run the program, and just select file1, file3, and file5, pressing Button1 will make them go into the right list box again. This is where my problem is: If you select all of the files now in the right list box (file1, file3, file3), and press Button2, matlab outputs file1, file2, and file3... not file1, file3 and file5 as I would like.
Now I understand why this is happening, because in a list box its value property starts at 1 and increases, and the underlying data in the Cell DataSet remains ordered file1 to file5... So value 1 2 3 refers to DataSet{1} DataSet{2} DataSet{3}...
What would be the best way to overcome this problem? I would like to add that for my actual GUI the file names might not always be so obviously named.
classdef example < handle
properties
Figure;
Button1;
Button2;
ListBox1;
ListBox2;
DataSet = {};
end
methods
function obj = example()
create(obj);
makeUpData(obj);
end
function create(obj)
obj.Figure = figure('Position',[300 300 640 640]);
obj.Button1 = uicontrol('Style','pushbutton','String','Button1',...
'Position',[240 260 80 40],'Callback',#obj.button1CB);
obj.Button2 = uicontrol('Style','pushbutton','String','Button2',...
'Position',[510 260 80 40],'Callback',#obj.button2CB);
obj.ListBox1 = uicontrol('Style','listbox','String','',...
'Position',[50 250 145 100],'Max',3);
obj.ListBox2 = uicontrol('Style','listbox','String','',...
'Position',[350 250 145 100],'Max',3);
end
function makeUpData(obj)
obj.DataSet = {'file1' 'file2' 'file3' 'file4' 'file5'};
obj.ListBox1.String = obj.DataSet;
end
function button1CB(obj,hObject,eventdata)
CurrentNum = get(obj.ListBox1,'Value');
NameListBox1 = get(obj.ListBox1,'String');
NewName = obj.ListBox2.String;
for i = 1:numel(CurrentNum)
NewName{end+1} = [NameListBox1{CurrentNum(i)}];
end
obj.ListBox2.String = NewName;
end
function button2CB(obj,hObject,eventdata)
CurrentNum = get(obj.ListBox2,'Value')
for i = 1:numel(CurrentNum)
obj.DataSet{CurrentNum(i)}
end
end
end
end
For this application I would recommend storing your data in a structure rather than a cell array. This will allow you to utilize dynamic field references to access your data and not worry about converting from a file name to an index in your cell array.
I've modified your properties definition:
properties
Figure;
Button1;
Button2;
ListBox1;
ListBox2;
DataSet = struct;
end
Your makeUpData definition:
function makeUpData(obj)
dummydata = {'file1' 'file2' 'file3' 'file4' 'file5'};
for ii = 1:length(dummydata)
obj.DataSet.(dummydata{ii}) = dummydata{ii};
end
obj.ListBox1.String = fieldnames(obj.DataSet);
end
And your button2CB definition:
function button2CB(obj,hObject,eventdata)
listboxvalues = get(obj.ListBox2, 'String');
CurrentNum = get(obj.ListBox2,'Value');
for i = 1:numel(CurrentNum)
obj.DataSet.(listboxvalues{CurrentNum(i)})
end
end
Now your selection returns:
ans =
file1
ans =
file3
ans =
file5
As expected.
You can change callback function of Button2 as follows, using ismember to get the corresponded index in the obj.DataSet:
function button2CB(obj,hObject,eventdata)
selected = cellstr(get(obj.ListBox2, 'String'));
referred = ismember(obj.DataSet, selected);
obj.DataSet(referred)
obj.DataSet{referred}
end

How to use inputs in an if statement in matlab

I'm trying to write a code in MATLAB that has the user input two values. I already have everything written for the input part and I saved the two inputs into two variables: value1 and value2.
What I'm trying to do is use the input values in the matter of:
if value1 = 2
output_result=10
if value1 = 3
output_result=20
and so on.
I've been trying to write an if-elseif statement but I can't seem to figure it out.
Do a switch statement
switch value1
case 2
result = 10;
case 3
result = 20;
...
otherwise
statements
end
If you really want to use an if statement, do this:
if value == 1
result = 10;
elseif value == 2
result = 20;
elseif
%// Put more statements
...
elseif
%// Put even MOAR statements
...
...
else
%// Default case - optional
end
However, the switch statement as per #kkuilla is more elegant. Also note that the else statement is optional. You'd only put this in if everything else fails and want to use a default case.

msgbox in MATLAB

I want to show my output in a msgbox so I have used msgbox(num2str(output)) but I want to name each line, eg:
Red 25
Green 52
Yellow 88
but when I try to do that it says
Error using horzcat
CAT arguments dimensions are not consistent.
And when that window pops up and the user press OK then another window will pop up asking
W = questdlg('Would you like to retrain or test the network?', ...
'Artificial Neural Network', 'Retrain', 'Test', 'Exit', 'Exit');
So, how can format my msgbox and as soon as the OK button is pressed another window will popup?
Any help would be appreciated!
Thanks!
For your first question, you can use cell array notation to format your message box text:
rVal = 25;
gVal = 35;
bVal = 45;
msg = {['Red ',num2str(rVal)];...
['Green ',num2str(gVal)];...
['Blue ',num2str(bVal)]};
This allows you to vertically concatenate multi-length strings.
If your output is an Nx1 column vector, you can always format it in this manner using cellfun:
output = [25;35;45];
msgTxt = {['Red '];['Green '];['Blue ']};
msgNum = cellfun(#num2str,num2cell(output),'UniformOutput',false);
msg = cellfun(#(x,y) [x,y],msgTxt,msgNum,'UniformOutput',false);
As long as you match the msgTxt size with the output size, this should work fine for any size of the output variable.
As for making your program wait on the user response, try uiwait:
mH = msgbox(msg);
uiwait(mH)
disp('Let''s continue...')
msgbox can be formatted like this
R=23;G=35;B=45; %given values
(msgbox({['Red ',num2str(R)];['Green ',num2str(G)];['Blue ',num2str(B)]; }));
After your later part of the question
uiwait(msgbox({['Red ',num2str(R)];['Green ',num2str(G)];['Blue ',num2str(B)]; }));
W = questdlg('Would you like to retrain or test the network?', ...
'Artificial Neural Network', 'Retrain', 'Test', 'Exit', 'Exit');

Close MATLAB GUI with a dialog

I'm doing a GUI with GUIDE and want to close the figure with a dialog.
I have a button with this
selection = questdlg('Close This Figure?',...
'Close Request Function',...
'Yes','No','Yes');
switch selection,
case 'Yes',
delete(gcf)
case 'No'
return
end
and it works fine but I want the main button to do the same thing.
I tryed to put this next to
function varargout = file_name(varargin)
...
but won't work. Any suggestions?
Use above code in a function say:
%Save in myclose_callback.m
function myclose_callback(src,evnt)
selection = questdlg('Close This Figure?',...
'Close Request Function',...
'Yes','No','Yes');
switch selection,
case 'Yes',
delete(gcf)
case 'No'
return
end
end
Then use:
%From other .m script or from command window
figure('CloseRequestFcn',#myclose_callback)
To make this as your default callback for all figures, use:
set(0,'DefaultFigureCloseRequestFcn',#myclose_callback)