Assigning multiple variables to inputdlg's output string
8 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi,
I need to have the user enter multiple values and then for each value to be assigned as a number to a variable.
The current code I have is:
prompt = {'Enter value 1','Enter value 2','3','4','5','6'};
dlg_title = 'Enter values';
num_lines = 1;
defaultans = {'1','5','5','7','2','11'}
Params = inputdlg(prompt, dlg_title, num_lines, defaultans);
[a,b,c,d,e,f] = str2double(Params{:});
However this gives the error:
Error using str2double
Too many input arguments.
Any help would be great, thanks.
0 Kommentare
Antworten (2)
Stephen23
am 13 Mär. 2017
prompt = {'Enter value 1','Enter value 2','3','4','5','6'};
defaultans = {'1','5','5','7','2','11'};
Params = inputdlg(prompt, 'MyTitle', 1, defaultans);
C = num2cell(str2double(Params));
[a,b,c,d,e,f] = C{:}
a =
1
b =
5
c =
5
d =
7
e =
2
f =
11
2 Kommentare
Jan
am 13 Mär. 2017
+1: Perfect. I'm not sure if the indirection over num2cell is faster, but it is much nicer than a subfunction with a loop and varargout.
Jan
am 12 Mär. 2017
Bearbeitet: Jan
am 13 Mär. 2017
data = str2double(Params);
If you really want to split this to 5 variables:
a = data(1);
b = data(2);
c = data(3);
d = data(4);
e = data(5);
But either use smarter names for the variables or keep them as a vector, if it matchs the underlying problem.
[EDITED] If you really want to split the vector to different variables, you can do this with a small function:
function varargout = SplitToVars(data)
if nargout ~= numel(data)
error('Number of inputs does not match number of outputs.');
end
for iVar = 1:numel(data)
varargout{iVar} = data(iVar);
end
end
2 Kommentare
Siehe auch
Kategorien
Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!