How can I pass an image to a figure ?

3 vues (au cours des 30 derniers jours)
Doug Leaffer
Doug Leaffer le 8 Mai 2025
Commenté : Adam Danz le 8 Mai 2025
Hello. How can I pass an image to a figure in a MATLAB script?
% Generate a wood grain pattern (example)
grain = rand(300, 400);
grain_thresholded = grain > 0.5; % Creates a binary pattern
% Apply color values to simulate wood grain
image(:,:,1) = grain_thresholded * 0.8 + (1 - grain_thresholded) * 0.4; % Red
image(:,:,2) = grain_thresholded * 0.6 + (1 - grain_thresholded) * 0.2; % Green
image(:,:,3) = grain_thresholded * 0.3 + (1 - grain_thresholded) * 0.1; % Blue
% Display the image
figure;
imshow(image);
The existing figure code is below. I want to replace the brown/tan RGB figure color with the above image color and pattern. Thanks for any help !
% Plot beam cross section
% A = base*h; % beam area, in^2
figure()
pgon = polyshape([0 0 23.5 23.5],[12 0 0 12]);
H = plot(pgon,'FaceColor',[0.666, 0.392, 0.196],'FaceAlpha',0.5); % use 0.666, 0.392, 0.196
% RGB: (202 164 114) = 0.79 0.643 0.447 wood color
H.LineStyle = '--';
H.EdgeColor = 'black';
hold on
axis equal
grid on
x6 =[0 23.5];
y6 =[6 6];
line(x6,y6,'Color','red','LineStyle','-.') % adds a line at 'c = 6'
title('Cross Section of Designed Beam')
xlabel('base (in)'); ylabel('height (depth) (in)')

Réponse acceptée

Adam Danz
Adam Danz le 8 Mai 2025
Modifié(e) : Adam Danz le 8 Mai 2025
Use image and specify the x and y limits instead of using polyshape.
% Generate a wood grain pattern (example)
grain = rand(300, 400);
grain_thresholded = grain > 0.5; % Creates a binary pattern
% Apply color values to simulate wood grain
I(:,:,1) = grain_thresholded * 0.8 + (1 - grain_thresholded) * 0.4; % Red
I(:,:,2) = grain_thresholded * 0.6 + (1 - grain_thresholded) * 0.2; % Green
I(:,:,3) = grain_thresholded * 0.3 + (1 - grain_thresholded) * 0.1; % Blue
figure()
image([0 23.5], [0 12], I, AlphaData=0.5)
hold on
axis equal padded
grid on
x6 =[0 23.5];
y6 =[6 6];
line(x6,y6,'Color','red','LineStyle','-.') % adds a line at 'c = 6'
title('Cross Section of Designed Beam')
xlabel('base (in)'); ylabel('height (depth) (in)')
  2 commentaires
Doug Leaffer
Doug Leaffer le 8 Mai 2025
Thanks Adam. Your code worked when I passed it to a function wood(b, h, c) -- which are my variables, and called the wood function from a .mlx live script. For some reason the code did not execute without errors if I simply copied it into the live script with no function call.
Adam Danz
Adam Danz le 8 Mai 2025
I just copied it into a live script in R2024b and it ran without any issues. If you continue to have this issue, I recommend contacting tech support. Glad it worked for you otherwise!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Elementary Polygons dans Help Center et File Exchange

Produits


Version

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by