Matlab Directory Stopped Finding File

7 visualizzazioni (ultimi 30 giorni)
Hi,
I'm using the following code to create a video from a series of matlab files in a specific folder. Each file contains a variable which is the "Frame". I start by counting the number of files in the folder which works well and I had this whole code working perfectly yesterday. However, now matlab seems to not be able to find my files...
My main matlab script is kept in the folder: "E:\Uni\Imaging\23-11" and the files I want to load are in the subfolder "E:\Uni\Imaging\23-11\8". When I run my code matlab now says it cannot find the file. Saying
"Unable to read file 'Rec--000008_10HZ_1.mat'. No such file or
directory."
Yet the file exists and works. When run:
load("8\Rec--000008_10HZ_1.mat", "Frame")
On the command window, this works fine.
Anyone know what might cause this issue?
Thank you
%% Experiment 08 - Water Jet (7)
fprintf("Starting Experiment 8 \n\n");
%Importing All Frames for Investigation 08
folder = 'E:\Uni\Imaging\23-11\8';
files = dir(fullfile(folder, '*.mat'));
N_frames = size(files,1) - 2;
directory = 'Rec--000008_10HZ_%d.mat';
t = 0:0.1:N_frames;
% Create a VideoWriter object to write the video out to a new, different file.
writerObj = VideoWriter("test_8.avi",'Uncompressed AVI');
writerObj.FrameRate = 10; % How many frames per second.
open(writerObj);
% Need to change from the default renderer to zbuffer to get it to work right.
% openGL doesn't work and Painters is way too slow.
set(gcf, 'renderer', 'zbuffer');
%% Create the movie.
% After this loop starts, BE SURE NOT TO RESIZE THE WINDOW AS IT'S SHOWING THE FRAMES, or else you won't be able to save it.
%Import frames
for frameIndex = 1:800 %To number of frames N_frames (after 800 frames are useless)
cla reset;
filename = sprintf(directory, frameIndex);
load(filename, "Frame"); %Check if files exists
%Absolute Frame Post Process
currentFrame = Frame - min(Frame(:));
imagesc(currentFrame);
caption = sprintf('Experiment 8: Frame #%d of %d, t = %.1f', frameIndex, N_frames, t(frameIndex));
title(caption, 'FontSize', 15);
colorbar
thisFrame = getframe(gcf);
%Write this frame out to a new video file.
writeVideo(writerObj, thisFrame);
end
close all;
close(writerObj);

Risposta accettata

Voss
Voss il 5 Apr 2022
Modificato: Voss il 5 Apr 2022
Maybe yesterday when you ran this your working directory was "E:\Uni\Imaging\23-11\8\" and today it is "E:\Uni\Imaging\23-11\".
This is why it is better to use absolute paths instead of relative paths, as you have discovered, according to your solution.
Use filename = fullfile(folder,sprintf(directory, frameIndex)); Explanation in comments below:
folder = 'E:\Uni\Imaging\23-11\8';
directory = 'Rec--000008_10HZ_%d.mat';
frameIndex = 1;
% a relative path: "Rec--000008_10HZ_1.mat"
% -> that file doesn't exist in "E:\Uni\Imaging\23-11\" but does exist in "E:\Uni\Imaging\23-11\8\"
% so if your working directory is "E:\Uni\Imaging\23-11\" this file will NOT be found,
% but if your working directory is "E:\Uni\Imaging\23-11\8\" this file will be found
filename = sprintf(directory, frameIndex)
filename = 'Rec--000008_10HZ_1.mat'
% an absolute path: "E:\Uni\Imaging\23-11\8\Rec--000008_10HZ_1.mat"
% that file is there and MATLAB will find it and it doesn't matter what
% your working directory happens to be at the moment
filename = fullfile(folder,sprintf(directory, frameIndex)) % ignore the "/" here, it'll be a "\" on your Windows machine
filename = 'E:\Uni\Imaging\23-11\8/Rec--000008_10HZ_1.mat'
filename = append(folder,"\",sprintf(directory, frameIndex))
filename = "E:\Uni\Imaging\23-11\8\Rec--000008_10HZ_1.mat"

Più risposte (1)

Afonso Espírito Santo
Afonso Espírito Santo il 5 Apr 2022
I have managed to now solve this issue. I added the following code just before load is called. Still don't understand teh original problem. If anyone does and can explain it would be greatly appreaciated.
filename = append(folder,"\",filename);

Categorie

Scopri di più su Startup and Shutdown in Help Center e File Exchange

Prodotti


Release

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by