My GUI is not reading an input array in the edit box.
Show older comments
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
Jan
on 20 Jun 2017
You should get an error message also. Please post it.
Evan Hiatt
on 21 Jun 2017
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.
Answers (1)
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)
1 Comment
Evan Hiatt
on 21 Jun 2017
Categories
Find more on Data Type Conversion 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!