For loop won't display all 10 images in the subplot
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
%% 1.1 - Load and Prepare Data Set
clear;
clc;
B = load('images.mat').image_data'; % Loads image file and transposes
C = mat2gray(B); % Converts images to grayscale
% Loop to generate and display images
for i = 1:10
im = reshape(C(:,i), [37, 50])'; % Reshape and store the image
subplot(10, 1, i); % Create a 10x1 grid of subplots
imshow(im, []); % Display each image with scaling
end
The above turns the colunms of a correctly sized matrix into images. When run I'm only getting 9 images of the neccessary 10. I'm thinking there's some quirk where the firs column isn't running but I'm unsure. I'm running it in matlab live in a browser window.
1 comentario
the cyclist
el 29 de Sept. de 2024
Can you upload the data? You can use the paper clip icon in the INSERT section of the toolbar.
Does the 10th axis get drawn, just not filled in? Maybe the last image is corrupted, or has just NaN? Do you get any warnings or errors?
Respuestas (1)
Arjun
el 30 de Sept. de 2024
I see that you want to display 10 images in a subplot but for some reason you can see only 9 of them.
To diagnose the issue, we can use the following steps:
- Check the size of the matrix “C”: Ensure that you don’t have any missing column problem.
- Replace any NaN values with 0: Using this you can ensure that there is no error due to introduction of NaN values in the data.
- Use debugging statement using “disp”: To ensure that the loop runs ten times, you can use the "disp" function to display the iteration number.
- Good practise to put a “title” to each image: If title is displayed and image is omitted then there may be some issue with the image itself.
- Consider to change the layout: Instead of using “subplot(10,1,i)” you can use “subplot(5,2,i)” for better view.
You can follow the below workaround for better understanding of the issue:
clear;
clc;
% Generate sample data to test the code
numImages = 10; % Number of images
imageHeight = 37; % Image height
imageWidth = 50; % Image width
% Generate random image data
% Each column in B represents a flattened image
B = rand(imageHeight * imageWidth, numImages);
% Convert images to grayscale
C = mat2gray(B);
% Replace any NaN values with 0
C(isnan(C)) = 0;
% Check the number of columns
[numRows, numCols] = size(C);
if numCols < 10
error('The dataset contains fewer than 10 images.');
end
% Display the images
figure;
for i = 1:numImages
disp(i);
im = reshape(C(:,i), [imageHeight, imageWidth])'; % Reshape and store the image
subplot(5, 2, i); % Create a 5x2 grid of subplots
imshow(im, []); % Display each image with scaling
title(['Image ', num2str(i)]); % Add a title to each subplot
end
Using the steps above you should be able to diagnose the issue in your code.
Here is a documentation link to detect NaN values using "isnan" function: https://www.mathworks.com/help/matlab/ref/double.isnan.html
I hope this helps!
0 comentarios
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!