Setting properties of some static text boxes based on contents of another variable

2 views (last 30 days)
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.

Accepted Answer

Steven Lord
Steven Lord on 4 Nov 2016
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.
  1 Comment
Matt
Matt on 7 Nov 2016
Edited: Matt on 7 Nov 2016
That did it, I see what I did wrong, thanks very much.
Dynamics field names is a helpful tip, thank you. I don't know how I didn't find that in my searches.

Sign in to comment.

More Answers (0)

Categories

Find more on Data Import and Export in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!