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