plot for mp4 file format

5 views (last 30 days)
mari jutt
mari jutt on 8 Feb 2020
Answered: sanidhyak on 10 Feb 2025
i can't get plot of mp4 file format, i always get this error
Error using plot
Data must be numeric, datetime, duration or an array convertible to double.
Error in Untitled (line 6)
plot(t,file)
file=VideoReader('video.mp4');
info=audioinfo('video.mp4')
t = 0:seconds(1/Fs):seconds(60);
t = t(1:end-1);
figure(1)
plot(t,file)
title('origional signal in time')
xlim([0, duration])
xlabel('time(s)')
ylabel('magnitude')

Answers (1)

sanidhyak
sanidhyak on 10 Feb 2025
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:
Cheers & Happy Coding!

Categories

Find more on Audio I/O and Waveform Generation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!