Comparing input character and a String

5 ビュー (過去 30 日間)
Yusuf Oguzhan Turgut
Yusuf Oguzhan Turgut 2019 年 6 月 10 日
コメント済み: Jan 2019 年 6 月 11 日
Hello, I am trying to design a GUI. In my GUI, Matlab gives 5 random characters and user will enter the same. I want to compare user's input and 5 characters that MATLAB gives for create a ratio called correction. correction ratio = how many characters are true / 5. For ex = if 1 character of MATLAB and user input is same, correction ratio = 1/5. I try to make this code but it gives error. How can i do that ?
function check_Callback(hObject, eventdata, handles)
% hObject handle to check (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
b1= get(handles.input,'String') % user's input
a1 = get(handles.q1,'String'); % 1st character of MATLAB's string
a2 = get(handles.q2,'String'); % 2nd character of MATLAB's string
a3 = get(handles.q3,'String'); % 3rd character of MATLAB's string
a4 = get(handles.q4,'String'); % 4th character of MATLAB's string
a5 = get(handles.q5,'String'); % 5th character of MATLAB's string
correction = 0;
for i = 1:5 %% this is for obtaining correction ratio.
if a(i)== char(b1(i))
correction = (correction + i)/5;
end
end
  2 件のコメント
Bob Thompson
Bob Thompson 2019 年 6 月 10 日
Please post the entire error message.
Yusuf Oguzhan Turgut
Yusuf Oguzhan Turgut 2019 年 6 月 10 日
a =
5
b1 =
'$>-&\'
Undefined function or variable 'a'.
Error in guioguzhan>check_Callback (line 353)
if a(i)== char(b1(i))
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in guioguzhan (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)guioguzhan('check_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.

サインインしてコメントする。

採用された回答

Jan
Jan 2019 年 6 月 10 日
Of course a(1) and a1 are two completely different things. It is not clear, what the contents of b1 is, but maybe you want:
b1 = get(handles.input,'String') % user's input
a{1} = get(handles.q1, 'String'); % 1st character of MATLAB's string
a{2} = get(handles.q2, 'String'); % 2nd character of MATLAB's string
a{3} = get(handles.q3, 'String'); % 3rd character of MATLAB's string
a{4} = get(handles.q4, 'String'); % 4th character of MATLAB's string
a{5} = get(handles.q5, 'String'); % 5th character of MATLAB's string
correction = 0;
for i = 1:5 %% this is for obtaining correction ratio.
if a{i} == b1{i}
correction = (correction + i) / 5;
end
end
  4 件のコメント
Yusuf Oguzhan Turgut
Yusuf Oguzhan Turgut 2019 年 6 月 10 日
Thank you very much :) It helped me a lot :)
Jan
Jan 2019 年 6 月 11 日
"b1 is like /()=!" is not clear enough. Prefer proper Matlab syntax instead:
b1 = '/()=!'
b1 = "/()=!"
b1 = {'/()=!'}
Maybe this is secure:
b1c = cellstring(b1);
b1_firstChar = b1c{1}(1);

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

タグ

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by