code won't run, but gives no error

10 vues (au cours des 30 derniers jours)
Shandilya
Shandilya le 2 Mai 2023
I have written the following code that involves multiple functions. Basically, I'm tracking a moving animal to export mobile and immobile into excel. Evidently this is wrong because the code is not running. However, I do not know what the problem is because it detects nothing wrong and gives no error message.
Here is the code:
close all
clear all
clc
% path of the folder containing the videos
videoFolder = 'currentFolder';r
videoFiles = dir(fullfile(videoFolder, '*.mp4'));
% loop through each video file and process them
for i = 1:length(videoFiles)
% loading the video file
video = VideoReader(fullfile(videoFolder, videoFiles(i).name));
% parameters for motion detection
threshold = 50; % pixel threshold for motion detection
numFrames = video.NumFrames;
frameRate = video.FrameRate;
immobileTime = 0;
% variables to store data
startTime = []; % start time of each mobile phase
endTime = []; % end time of each mobile phase
% loop through each frame of the video
for j = 1:numFrames
frame = read(video, j);
grayFrame = rgb2gray(frame);
if j > 1
diffFrame = abs(grayFrame - prevFrame);
else
diffFrame = zeros(size(grayFrame));
end
motionMask = diffFrame > threshold;
numPixelsWithMotion = sum(motionMask(:));
percentPixelsWithMotion = numPixelsWithMotion / numel(motionMask);
if percentPixelsWithMotion < 0.05 % rodent immobile if less than this
immobileTime = immobileTime + (1 / frameRate); % add the duration of the immobile phase
else % more than 0.05, its mobile
startTime = [startTime, j];
if j > 1 % if this is not the first mobile phase
endTime = [endTime, j-1];
end
end
prevFrame = grayFrame;
end
endTime = [endTime, numFrames];
mobileTime = endTime - startTime;
outputFile = fullfile(videoFolder, [videoFiles(i).name(1:end-4) '_results.csv']);
resultsTable = table(mobileTime', immobileTime, 'VariableNames', {'MobileTime_ms', 'ImmobileTime_s'});
writetable(resultsTable,outputFile);
end

Réponse acceptée

Walter Roberson
Walter Roberson le 2 Mai 2023
Modifié(e) : Walter Roberson le 2 Mai 2023
Either there is no folder in the current directory that is literally named currentFolder or else the folder literally named currentFolder exists but has no .mp4 files.
Note that in your code 'currentFolder' will not be replaced by the name of the current folder: it will be treated as the character vector ['c' 'u' 'r' 'r' 'e' 'n' 't' 'F' 'o' 'l' 'd' 'e' 'r']
  2 commentaires
Shandilya
Shandilya le 4 Mai 2023
So I've tried 3 things:
  1. Changed the directory by adding filepath --> videoFolder = '/Users/shandi/Desktop/Dr Sareesh';
  2. Double checked if any of the videos were corrupted and none of them were corrupted. All the videos are in .mp4
  3. Though this is a long shot, I resetted Matlab thinking it could be playing up
The end result is still the same "code won't run but doesn't give any error". Not sure what I'm missing
Walter Roberson
Walter Roberson le 4 Mai 2023
Please try this code and report back the kind of information it tells you
videoFolder = '/Users/shandi/Desktop/Dr Sareesh';
if ~exist(videoFolder, 'dir')
error('videoFolder directory "%s" does not exist', videoFolder);
end
videoFiles = dir(fullfile(videoFolder, '*.mp4'));
numfiles = length(videoFiles);
if numfiles == 0
error('videoFolder directory "%s" has no .mp4 files', videoFolder);
end
for i = 1 : numfiles
thisfile = fullfile(videoFiles(i).folder, videoFiles(i).name);
if ~exist(thisfile, 'file')
warning(sprintf('file found in directory but reportedly does not exist: "%s"', thisfile));
else
created_reader = false;
try
video = VideoReader(thisfile);
created_reader = true;
catch ME:
end
if created_reader
fprintf('reader okay for file "%s"\n', thisfile);
else
warning(sprintf('failed creating reader for file "%s"', thisfile));
end
clear video
end
end

Connectez-vous pour commenter.

Plus de réponses (1)

Shandilya
Shandilya le 8 Mai 2023
Déplacé(e) : Walter Roberson le 9 Mai 2023
once i ran this code, i've had 2 errors
  1. Error: File: tracking_mark4.m Line: 23 Column: 18. Invalid expression. Check for missing or extra characters. --> this would be "catch ME:" so i removed the colon to see if the code would run and this is when i got the 2nd error
  2. Error using tracking_mark4 (line 4). videoFolder directory "/Users/shandi/Desktop/Dr Sareesh" does not exist --> which is really wierd so i double checked the pathname through mac terminal which also stated "file path does not exist.
This is when a lightbulb moment came about, it turns out when giving the exact pathname to terminal, it reads Dr Sareesh as 2 different folder names which is wierd so i changed the folder name to "FST" and the code you had provided worked like a charm. Thank you so much!!
Going back to my original code, changing the pathname seemed to run the code and it seems to all work fine as of now. I was able to get my immobile time in the units i require for the analysis.
Thank you so much for your help! Really appreciate it!!
  1 commentaire
Walter Roberson
Walter Roberson le 9 Mai 2023
Spaces in folder names can indeed cause problems unless you are careful with quoting or escaping the spaces.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Startup and Shutdown dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by