How to create a simple single color visualization in simulink ?

3 ビュー (過去 30 日間)
Michael
Michael 2014 年 12 月 16 日
編集済み: prabhat kumar sharma 2024 年 12 月 12 日
Hi,I'm trying to visualize a single color in a simulink simulation. I would like to send a vector of 3 values [R,G,B] and simply have an that color displayed and updated live on some sort of screen/plot.
I have tried inserting an "matlab function" block and using this function:
function drawColor(RGB)
rectangle('FaceColor',RGB)
This works and displayes the color just like I want it to, but the display is updated very slowly.
Is there a proper way to do this ?

回答 (1 件)

prabhat kumar sharma
prabhat kumar sharma 2024 年 12 月 12 日
編集済み: prabhat kumar sharma 2024 年 12 月 12 日
Hi Michael,
The slow update you're experiencing might be due to the overhead of repeatedly creating graphics objects.
Here is another approach you can try.
  • Create a MATLAB Function block in your Simulink model.
  • Use the following code in the MATLAB Function block:
function drawColor(R, G, B)
coder.extrinsic('set', 'rectangle', 'gcf', 'cla');
persistent rect;
if isempty(rect)
% Create the rectangle once
figure('Name', 'Color Display', 'NumberTitle', 'off');
rect = rectangle('Position', [0, 0, 1, 1], 'FaceColor', [R, G, B]);
axis off; % Turn off the axes for a cleaner look
else
% Update the rectangle's color
set(rect, 'FaceColor', [R, G, B]);
drawnow limitrate; % Optimize rendering speed
end
end

カテゴリ

Help Center および File ExchangeModeling についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by