Creating input options based on a number

7 views (last 30 days)
Just say I put in 7 for the amount, how would the input option print out seven b and seven c.
I can't figure out a efficient way to do it
  5 Comments
Oleg Komarov
Oleg Komarov on 24 Mar 2012
To keep it simple you can call inputdlg once asking for how many fields, then call it again setting the number of fields.
Rooy
Rooy on 24 Mar 2012
IS there a way when I enter a number in the first collum of the input dialog the others expand to fit that number, if it not possible it alright. thank you for the help

Sign in to comment.

Accepted Answer

Oleg Komarov
Oleg Komarov on 24 Mar 2012
There is but you have to program it yourself:
  • write completely the whole GUI, using also jTextField (java) in place of the MATLAB edit box.
To me it seems an overkill unless your primary task is to program a GUI.
I would simply go for:
n = input('How many a and b: ');
out = NaN(n,2);
r = 0;
tmp = input('Enter values for a: ');
while r < n
tmp = input(repmat(' ',1,20));
if isempty(tmp)
continue
else
r = r+1;
out(r,1) = tmp;
end
end
r = 0;
tmp = input('Enter values for b: ');
while r < n
tmp = input(repmat(' ',1,20));
if isempty(tmp)
continue
else
r = r+1;
out(r,1) = tmp;
end
end
EDIT I decided to show you how much of an overkill is, hope you'll enjoy it :)
function varargout = myGUI2
% Syntax examples:
% myGUI2 -> returns both values for 'a' and 'b' in 'ans'
% out = myGUI2 -> returns both values for 'a' and 'b' in 'out'
% [out1,out2] = myGUI2 -> returns 'a' in 'out1' and 'b' in 'out2'
% [...,...,S] = myGUI2 -> returns also structure with handles
S.fh = figure( 'units', 'pixels',...
'position', [500 500 200 260],...
'menubar', 'none',...
'name', 'myInputdlg',...
'numbertitle', 'off',...
'resize', 'off',...
'color', [.94 .94 .94]);
% Edit box: how many a and b
S.ed = uicontrol('style', 'edit',...
'unit', 'pix',...
'position', [10 205 70 30],...
'background', 'w');
% Text: on top of edit box
S.tx = uicontrol('style', 'text',...
'string', 'How many values for a and b:',...
'unit', 'pix',...
'position', [10 240 180 15],...
'background', [.94 .94 .94],...
'Horizontal', 'left');
% Pushbutton: refresh the uitable that accepts values for a and b
S.pb(1) = uicontrol('style', 'push',...
'units', 'pix',...
'position', [91 208 100 25],...
'string', 'Refresh',...
'callback', @pb_call_refresh);
% Text: warns if other than numeric values
S.tx(2) = uicontrol('style', 'text',...
'unit', 'pix',...
'position', [10 175 180 25],...
'background', [.94 .94 .94],...
'foreground', 'r');
% Uitable: values for a
S.ut(1) = uitable( 'unit', 'pix',...
'position', [10 45 85 135],...
'ColumnF', {'numeric'},...
'ColumnW', {66},...
'ColumnN', 'a',...
'Enable', 'inactive',...
'RowName', [],...
'ColumnE', true);
% Uitable: values for b
S.ut(2) = uitable( 'unit', 'pix',...
'position', [106 45 85 135],...
'ColumnF', {'numeric'},...
'ColumnW', {66},...
'ColumnN', 'b',...
'Enable', 'inactive',...
'RowName', [],...
'ColumnE', true);
% Pushbutton: done
S.pb(2) = uicontrol('style', 'push',...
'units', 'pix',...
'position', [50 10 100 25],...
'string', 'Done',...
'callback', @pb_call_done);
S.tmp = {};
uiwait(S.fh)
if nargout > 1
varargout = S.tmp;
else
varargout = {cell2mat(S.tmp.')};
end
if nargout == 3
varargout{3} = S;
end
if ishghandle(S.fh)
delete(S.fh)
end
% pb_call Refresh
function pb_call_refresh(varargin)
nrows = get(S.ed,'String');
if isempty(nrows)
return
elseif regexp(nrows,'\D')
set(S.tx(2),'String','Only numeric values [0-9]!')
set(S.ut,'enable','off')
else
set(S.tx(2),'String','')
nrows = str2double(nrows);
set(S.ut,{'Data'},{nan(nrows,1)},{'enable'},{'on'})
end
end
% pb_call Done
function pb_call_done(varargin)
S.tmp = get(S.ut,'Data');
uiresume(S.fh)
end
end

More Answers (0)

Categories

Find more on Desktop in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!