MATLAB: Appdesigner create loop for drop down Items

app designerdrop downlookup tableplottingwhile loop

Hello,
I am using app designer to import data from a csv file an plot curves, based on the data from the file, in UIAxes. I'd like to implemet the function to change the colour of each curve. Therfore, I use a drop down field, filled with 8 different colours. These colours are defined in the private properties area (RGB values).
LightBlue = [0.3010 0.7450 0.9330];
DarkBlue = [0 0.4470 0.7410];
LightRed = [0.8500 0.3250 0.0980];
DarkRed = [0.6350 0.0780 0.1840];
Yellow = [0.9290 0.6940 0.1250];
Purple = [0.4940 0.1840 0.5560];
Green = [0.4660 0.6740 0.1880];
Black = [0 0 0];
After changing the dropdownfield-value, the user has to confirm his changes. To use a short code, I want to create a while-loop:
A = [app.ColourDropDown_1.Value app.ColourDropDown_2.Value app.ColourDropDown_3.Value app.ColourDropDown_4.Value app.ColourDropDown_5.Value app.ColourDropDown_6.Value app.ColourDropDown_7.Value app.ColourDropDown_7.Value];
B = [app.ColourCurve1 app.ColourCurve2 app.ColourCurve3 app.ColourCurve4 app.ColourCurve5 app.ColourCurve6 app.ColourCurve7 app.ColourCurve8];
C = 1;
while C<9
switch A(1,C)
case 'Light blue'
B(1,C) = app.LightBlue;
case 'Dark blue'
B(1,C) = app.DarkBlue;
case 'Light Red'
B(1,C) = app.LightRed;
case 'Dark Red'
B(1,C) = app.DarkRed;
case 'Yellow'
B(1,C) = app.Yellow;
case 'Purple'
B(1,C) = app.Purple;
case 'Green'
B(1,C) = app.Green;
case 'Black'
B(1,C) = app.Black;
end
C = C+1;
end
In the first iteration, the code should take the first value of matrix A (drop down field value). Then, it should take the first value from matrix B and allocate the colour-value.
Does anyone know a way to do that? I know the problem in my code, but is there another way than do the switch-case code for every chanel?
Thank you very much!

Best Answer

See inline comments for instructions
Option 1) Switch case
A = [app.ColourDropDown_1 app.ColourDropDown_2 app.ColourDropDown_3 ...
app.ColourDropDown_4 app.ColourDropDown_5 app.ColourDropDown_6 ...
app.ColourDropDown_7 app.ColourDropDown_7]; % These are handles now; ".Value" removed.
B = [app.ColourCurve1 app.ColourCurve2 app.ColourCurve3 ...
app.ColourCurve4 app.ColourCurve5 app.ColourCurve6 ...
app.ColourCurve7 app.ColourCurve8];
colors = nan(numel(B,3)); % Preallocate color matrix
for i = 1:numel(B) % use a for loop instead of a while loop.
switch lower(A(C).Value) % Assumes the Value properties in A return strings/char-vectors; lower() used to avoid problem with case mismatch
case lower('Light blue') % lower() used to avoid problems with case mismatch
colors(i,:) = app.LightBlue; % store 1x3 color vector
case lower('Dark blue')
colors(i,:) = app.DarkBlue;
case lower('Light Red')
colors(i,:) = app.LightRed;
case lower('Dark Red')
colors(i,:) = app.DarkRed;
case lower('Yellow')
colors(i,:) = app.Yellow;
case lower('Purple')
colors(i,:) = app.Purple;
case lower('Green')
colors(i,:) = app.Green;
case lower('Black')
colors(i,:) = app.Black;
otherwise % If no cases are matched, assign black.
colors(i,:) = app.Black;
% error('Case not recognized.') % or you can throw an error
end
end
set(B, {'Color'}, mat2cell(colors,ones(numel(B),1),3)) % assign colors to all lines
Option 2) lookup table
Another cleaner way for you to do this would be to use a lookup table; no loops needed.
% General idea... not tested
colorNames = get(A,'Value'); % cell array of color names in order of A
% Look up color values
colorKey = {'LightBlue', [0.3010 0.7450 0.9330];
'DarkBlue', [0 0.4470 0.7410];
'LightRed', [0.8500 0.3250 0.0980];
'DarkRed' , [0.6350 0.0780 0.1840];
'Yellow' , [0.9290 0.6940 0.1250];
'Purple' , [0.4940 0.1840 0.5560];
'Green' , [0.4660 0.6740 0.1880];
'Black' , [0 0 0]};
[~, colorRowIdx] = ismember(lower(colorNames),lower(colorKey(:,1)));
colors = colorKey(colorRowIdx,2);
set(B, {'Color'}, colors)