Info

この質問は閉じられています。 編集または回答するには再度開いてください。

How can i update the color of my initial band of my resistor?

1 回表示 (過去 30 日間)
Tomas Negasi
Tomas Negasi 2018 年 12 月 11 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
Hello,
i already tried a bit but at some point i cant come further. I already got my initial bands and the value
for them but the next step would be to update the color of my bands so that when i chose the color value in the popupmenu
the right image pops up. I already did the initial bands with color images that had in my file
This is how the bands look now but i need to update the color of my bands
which i dont know how to do. I tried it with this code but it didnt work.
Can somebody please help me i really want it to work.
Thank you very much for every help

回答 (1 件)

Nick
Nick 2018 年 12 月 11 日
Its hard to tell how you implemented it based on only those screenshots. But the idea is relatively simpel:
You add a callback to your dropdowns, in that callback you grab the color that is linked to the dropdown value. Then you update the axes/image with that color
Below is a small example that illustrates the idea of how this could work, out of lazyness i just used a nested function to share the handles variable. It might be that its a property of a class or the variable that gets passed around in GUIDE in your situation.
function exampleCode
% create a struct that will be used directly inside the nested function - to make example work out of the box without boilerplate
handles = struct;
% initialize example gui
handles.fig = figure;
handles.axes.left = axes(handles.fig, 'units','norm','Position',[0.1 0.5 0.3 0.4]);
handles.axes.right = axes(handles.fig, 'units','norm','Position',[0.6 0.5 0.3 0.4]);
handles.dropdown.left = uicontrol(handles.fig, 'Style','popupmenu','String',{'blue','green', 'red'},'units','norm','Position',[0.1 0.3 0.3 0.18], 'Callback',@updateColor, 'Tag','left');
handles.dropdown.right = uicontrol(handles.fig, 'Style','popupmenu','String',{'blue','green', 'red'},'units','norm','Position',[0.6 0.3 0.3 0.18], 'Callback',@updateColor, 'Tag','right');
handles.colorSize = [500 500 3];
% set initial colors 2x blue
initialColor = grabColor('blue', handles.colorSize);
imshow(initialColor, 'Parent', handles.axes.left);
imshow(initialColor, 'Parent', handles.axes.right);
function updateColor(src, ~)
color = grabColor(src.String{src.Value}, handles.colorSize);
switch src.Tag
case 'left'
parentAx = handles.axes.left;
case 'right'
parentAx = handles.axes.right;
end
parentAx.Children.CData = color;
% you could imshow every time, which is quite a bit slower but still fast enough for what you try to achieve here
% it would look like this:
% imshow(color, 'Parent', parentAx);
end
end
function color = grabColor(colorId, colorSize)
% depending on the amount of colors you could just work with imread here
% but make sure you do a check for the size and resize accordingly
switch colorId
case 'green'
% could be color = imread('pathtogreen.jpg')
green(:,:,3) = zeros(500,500);
green(:,:,1) = zeros(500,500);
green(:,:,2) = 255*ones(500,500);
color = uint8(green);
case 'blue'
blue(:,:,3) = 255*ones(500,500);
blue(:,:,1) = zeros(500,500);
blue(:,:,2) = zeros(500,500);
color = uint8(blue);
case 'red'
red(:,:,3) = zeros(500,500);
red(:,:,1) = 255*ones(500,500);
red(:,:,2) = zeros(500,500);
color = uint8(red);
end
if ~all(size(color)==colorSize)
if size(color,3) < 3
% should be rgb but incase you load in a gray image with only one channel
repmat(color(:,:,1), 1, 1, 3);
end
color = imresize(color, [colorSize(1), colorSize(2)]);
end
end
Hopefully this example can set you on your way to solve your problem.

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by