How to convert a string to number in gui
2 ビュー (過去 30 日間)
古いコメントを表示
Hi everbody!
So I have a gui. In this GUI, I have an editable textbox, that when I input data in it, I would be expecting an action from a push button. I add the code to make it clearer.
function plot_Callback(~, ~, handles)
% hObject handle to plot (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
caseselection= get(handles.Cases, 'Value');
typeselection= get(handles.type, 'String');
if caseselection==2 && typeselection=='5'
cla(handles.axes, 'reset')
axes(handles.axes)
time= 0:1:40;
case1t5= [5 6 9 14 21 30 41 54 69 86 105 126 149 174 201 230 261 294 329 366 405 446 489 534 581 630 681 734 789 846 905 966 1029 1094 1161 1230 1301 1374 1449 1526 1605];
plot(time,case1t5)
grid on
title('case1');
xlabel('time')
ylabel('output');
elseif caseselection==2 && typeselection=='10'
cla(handles.axes, 'reset')
axes(handles.axes)
time= 0:1:30;
case1t10=[5 6 8.580100284 12.54923785 17.81711804 24.32434614 32.02702857 40.89057314 50.88656794 61.99099212 74.18309709 87.44466178 101.7594726 117.1129469 133.4918511 150.8840853 169.2785149 188.664836 209.033467 230.3754598 252.6824255 275.9464732 300.160157 325.316432 351.4086155 378.4303537 406.3755931 435.2385548 465.0137125 495.6957722 527.2796551];
plot(time,case1t10)
grid on
title('case1');
xlabel('time')
ylabel('output');
the problem pops up with the second condition.
Thx!
2 件のコメント
採用された回答
dpb
2019 年 8 月 7 日
Aha! OK, now I see the problem...indeed, '10' is a 1x2 char array and so it is a vector and the short-circuit logical operators can only operate on scalars. I didn't notice that originally....was thinking of the result of the textbox as being a set of values trying to return and the data vectors were all constants. Anyway--for your case you need something like
caseselection==2 && str2num(typeselection)==10
for the logic expressions...if caseselection is also from a textbox or other gui component that returns string result instead of numeric, it needs the conversion first, too, of course.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Interactive Control and Callbacks についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!