Appdesigner create loop for drop down Items

11 ビュー (過去 30 日間)
Laurenz Grigat
Laurenz Grigat 2021 年 1 月 25 日
編集済み: Adam Danz 2021 年 1 月 26 日
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!
  1 件のコメント
Mario Malic
Mario Malic 2021 年 1 月 25 日
編集済み: Mario Malic 2021 年 1 月 25 日
Table is the better way to store the colors and RGB values.
You can also consider line properties, most of these colors (if not all are available without RGB values - see line colors).
What's the purpose of having so many dropdown menus, you'll have to copy paste your callbacks plenty of times (which is not an issue), would it be acceptable for you if you'd have two dropdown menus with button to apply the color to selected line?
Do you have confirm button for every single line? From the description I can guess that you have 7 dropdowns for lines, and each of the dropdown consists of 8 colors, is this correct? Picture would be good as well. Where do you have apply button?
Will edit this bottom part tomorrow:
Your callback should be similar to this
function Dropdownvaluechanged(Source, Event)
% get the values on the both dropdowns
ddValue = Source.Value; % Gets the selected
ddRow = find(strcmpi(ddValue,Source.Items)) % Gets the row index of selected color
end

サインインしてコメントする。

採用された回答

Adam Danz
Adam Danz 2021 年 1 月 25 日
編集済み: Adam Danz 2021 年 1 月 26 日
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)

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by