Hi mari,
I understand that you are trying to get a plot of an MP4 file but are facing the error: “Error using plot - Data must be numeric, datetime, duration or an array convertible to double”. This error occurs because “file = VideoReader('video.mp4')” creates a “VideoReader” object, not numeric data. The “plot” function requires numeric or time-series data, but file is an object, causing the issue.
To resolve this error, I am assuming that you are either looking to plot pixel intensities over time or visualize audio signals over time, as these are the most relevant aspects when plotting an MP4 file.
For plotting pixel intensities over time:
- Use the “VideoReader” function to extract frames.
- Convert frames to grayscale using “rgb2gray” function.
- Compute the mean intensity for each frame.
- Plot the mean intensity over time to visualize brightness changes.
Kindly refer to the following code for the same:
% Read video file
video = VideoReader('video.mp4');
% Get video properties
numFrames = video.NumFrames;
frameRate = video.FrameRate;
% Initialize time vector
t = (0:numFrames-1) / frameRate;
% Extract intensity values (e.g., grayscale average)
frameIntensities = zeros(1, numFrames);
for k = 1:numFrames
frame = read(video, k); % Read frame
grayFrame = rgb2gray(frame); % Convert to grayscale
frameIntensities(k) = mean(grayFrame(:)); % Compute average intensity
end
% Plot the frame intensity over time
figure;
plot(t, frameIntensities, 'LineWidth', 1.5);
title('Video Frame Intensity Over Time');
xlabel('Time (s)');
ylabel('Average Intensity');
grid on;
Alternatively, for plotting audio signals of an MP4 file over time:
- Extract audio from the MP4 file using “audioread” function.
- Generate a time vector based on the sampling rate.
- Plot the audio signal over time.
Kindly refer to the following code for the same:
% Read audio data from the MP4 file
[audioData, Fs] = audioread('video.mp4');
% Generate time vector
t = (0:length(audioData)-1) / Fs;
% Plot the audio signal
figure(1)
plot(t, audioData)
title('Original Signal in Time Domain')
xlabel('Time (s)')
ylabel('Magnitude')
xlim([0, max(t)])
For further reference on the MATLAB functions used here, please refer to the documentation below:
- audioread: https://www.mathworks.com/help/matlab/ref/audioread.html
- videoreader: https://www.mathworks.com/help/matlab/ref/videoreader.html
- read: https://www.mathworks.com/help/matlab/ref/matlab.io.datastore.read.html
- plot: https://www.mathworks.com/help/matlab/ref/plot.html
Cheers & Happy Coding!
