My GUI is not reading an input array in the edit box.

I am trying to make a very simple GUI that graphs the graph of capacitance vs Distance in physics. All the GUI has is two edit boxes, a push button, and an axes. I want to have the first edit box read in an array (the distances) and the second edit box to take in a single number. When I run the GUI I input the array 1:100 into the first edit box and then the number 1000 into the second edit box. note:(I changed their tags to input1 and input2) ... Anyway when I put that in and hit the push button, it wont graph and I am just getting that error sound. I was able to make a GUI that will graph when I hard code numbers into the code instead of using the inputs from the edit box. So my guess is the problem is there.
My Code under the push button callback:
D = str2num(get(handles.input1, 'String'));
A = str2num(get(handles.input2, 'String'));
E = 8.854e-12;
C=D.\(E*A);
% D = 1:100; %These two commented out lines were used to make a graph without using the inputs
% C = D.\(1000*E);
axes(handles.axes1);
plot(D,C);

3 Comments

You should get an error message also. Please post it.
%I just copied and pasted this error message that comes up in the command window.
Trial>> CapVSDistAPP Error using str2num (line 31) Requires character vector or array input.
Error in CapVSDistAPP>pushbutton1_Callback (line 81) D = str2num(get(handles.input1, 'String'));
Error in gui_mainfcn (line 95) feval(varargin{:});
Error in CapVSDistAPP (line 42) gui_mainfcn(gui_State, varargin{:});
Error in matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)CapVSDistAPP('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
1:100
will not work with str2num. You can only convert numbers as strings to numbers as doubles (incidentally str2double is better) using this. You cannot evaluate an expression which is what the above is and then have that converted to a number.
Probably there is a method using eval to achieve this, but I never use eval so I'll leave someone else to muddy themselves in the murky waters of that particular function!
Or if you will always want to use a contiguous array just use 2 edit boxes, one for the min or the range, one for the max and then create the array yourself from these converted to numbers.

Sign in to comment.

Answers (1)

Jan
Jan on 21 Jun 2017
Edited: Jan on 21 Jun 2017
If you know for sure, that the input in the text field is something like 1:100 (pattern: '%g:%g') you can create a dedicated conversion:
% Replace: D = str2num(get(handles.input1, 'String'));
Str = get(handles.input1, 'String');
Num = sscanf(Str, '%g:%g');
D = min(Num):max(Num); % Or Num(1):Num(2)

Categories

Asked:

on 20 Jun 2017

Commented:

on 21 Jun 2017

Community Treasure Hunt

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

Start Hunting!