Matlab GUI not recognizing variables

조회 수: 1 (최근 30일)
Huda M
Huda M 2022년 3월 15일
편집: Jan 2022년 3월 15일
Hello;
I build a Matlab GUI Code, but I have a problem when I do run it did not recognize variables values to find the row and column of that value.
as seen below;
  댓글 수: 2
Jan
Jan 2022년 3월 15일
편집: Jan 2022년 3월 15일
What does "it did not recognize variables values" mean exactly?
Please post code as text, such that the readers can suggest modifications using copy&paste. Screenshots makes it much harder to wrie an answer.
What is the purpose of the code? Why do you create variables in the base workspace? What is not working as expected?
After:
assignin('base', 'G91449166', 1);
the variable with this name is created in the command window. If you type there:
disp(G91449166)
1 is replied. But this is rarely useful for the GUI.
Huda M
Huda M 2022년 3월 15일
I want the input inside the Edit text to be text such as for examble G91469166 and then it will recognize that the value for that variable is 1 also for the other Text box if I wrote G91967188 and then it will recognize that the value for that variable is 2 then can find the row and the column inside the table data(a0,a1)>>> which are data(1,2)
here is the code
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
data=xlsread('COM FM 1%.xlsx');
set(handles.table1,'Data',data)
assignin('base','G91469166',1)
assignin('base','G91967188',2)
assignin('base','G91560900',3)
assignin('base','G91518203',4)
a0=str2double(get(handles.input1,'string'));
a1=str2double(get(handles.input2,'string'));
if data(a0,a1)==1
set(handles.input3,'string','No Wash')
else
set(handles.input3,'string','Wash')
end

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2022년 3월 15일
Make sure you are consistent about how you use x and y . MATLAB prefers y as the row number, because rows are vertical and y is traditionally vertical. MATLAB prefers x as the column number, because columns increase horizontally and x is traditionally horizontal.
If you are expecting that the string G91468166 in handles.input1.string will be interpreted as numeric 1 because you did that assignin() base, then you have not taken into account a couple of things:
  1. str2double() never pays attention to variables that are set. str2double() pays attention only to the immediate text, and whether the whole of it forms a valid numeric literal. The character vector G91468166 does not form a numeric literal such as 3.8135e-5 so str2double() would say it fails, and convert it to NaN
  2. the functions that do pay attention to variable names, such as eval(), do not specifically look in the base workspace: they look in the workspace of the current function. The exception is if you use evalin('base')
I suggest you use a different approach. I suggest you use a drop-down list, with the elements arranged in-order. Then instead of looking at the String propertly of a pushbutton, you look at the Value property of the drop-down list, and that will give you the index immediately (just make sure you check whether the Value is [] for the case where nothing has been selected yet.)

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by