Setting properties of some static text boxes based on contents of another variable
2 次查看(过去 30 天)
显示 更早的评论
Hi all,
I have a GUI interface that warns a user if they try to save the data to a .MAT without filling all boxes.
handles.DataRequiredToSave = {'m','wdist','wb','h',..............};
% Check that handles structure contains all data that is required to save the item
% being created
% If it exists in the handles structure returns a 1, else returns a 0.
exists_or_not = isfield(handles,handles.DataRequiredToSave);
% Test if all returned in the previous lines are 1s
% If all exist returns 1, else returns 0.
all_exist = all(exists_or_not);
% If all data required doesn't exist, throw error message, else begin saving process.
if all_exist == 0
msgbox('Data missing - please check all fields have been filled.',
'Data Error','error');
% Highlight variables that are missing red
for datacounter = 1:length(handles.DataRequiredToSave)
if exists_or_not(1,datacounter) == 0
handlename = ['handles.text',num2str(datacounter)]
set({handlename},'ForegroundColor',[1 0 0])
end
end
else
% Save Data
There are a lot of items in DataRequiredToSave so I am trying to set the colour of the textboxes next to the edit boxes (tagged text1, text2 etc.) using a for loop, dynamically changing the handle that the set function uses in order to only set the static text for the data fields that have been accidentally left empty.
This gives me an error on the set line:
Error using set
Conversion to double from cell is not possible.
Error in Product_Data>Save_As_Menu_Callback (line
196)
set({handlename},'ForegroundColor',[1 0 0])
What am I doing wrong? Whatever I do I can't get the changing of the handle I am trying to set the properties of to work.
Thanks in advance, Matt.
0 个评论
采纳的回答
Steven Lord
2016-11-4
Passing a cell array containing a char vector into the set function as the first input won't work. What will work is:
handlename = handles.(['text',num2str(datacounter)])
set(handlename, 'ForegroundColor', [1 0 0])
This uses the dynamic field names feature to retrieve the appropriate field of the handles structure.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Import and Export 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!