Reading files from different directories

1 view (last 30 days)
Bojan Poletan
Bojan Poletan on 10 May 2022
Commented: Bojan Poletan on 12 May 2022
I am new with Matlab, want to read a file from a different directory, see below for the hirarchy:
Project_Folder/
->dat/
-> file1.dat
->src/
-> myFile.m
I must read
file1.dat
from
myFile.m
so I type (as found online):
../dat/file1.dat
But it says the file does not exist but if I put it in the same folder and type just
file1.dat
it works! (I am on a MACOS)
More exactly this is the error I get:
File does not exist: ../dat/controlled_config_1_train.dat
Error opening file: Invalid file identifier. Use fopen to generate a valid file identifier.
Output argument "Hist" (and maybe others) not assigned during call to "dct_coef_hist_from_list".
Here is the directory view:
Thanks for your help!
Here is the peace of code:
% file is defined as a parameter and contains a string like: "dat/file1.dat"
function [Hist, file_path] = dct_coef_hist_from_list(file)
% This function extracts DCT coefficient histograms of images provided in
% a file
% Input:
% file: the file containing all paths
% channel: 1 (luminance), 2 (chrominance)
% Nc: number of analyzed DCT coeff in each 8x8 blocks
% BT: the limit of histogram ==> number of bins = 2*BT + 1
% Output:
% Hist: the nxd histogram matrix; n is the number of images; d is the
% number of histogram bins
% file_path: a list containing file paths (used to split data later)
Nc = 9; % number of AC coefficients (zig zag scan)
BT = 20; % maximum bin value => number of bins = 2*BT+1
if exist(file, "file")
fprintf("File exists: %s\n", file);
else
% this message is displayed
fprintf("File does not exist: %s\n", file);
end
try
fid = fopen(file, 'r'); % here the code fails
file_path = textscan(fid, '%s', 'delimiter', '\n', 'whitespace', '');
file_path = file_path{1};
N = length(file_path);
fprintf('\nNumber of images: %d \n', N);
Hist = zeros(N, (2*BT+1)*Nc);
for i = 1:N
[~,file_name,ext] = fileparts(file_path{i});
fprintf('process file: %s\n', [file_name ext]);
fprintf('process file: %s\n', file_path{i});
Hist(i,:) = dct_coef_hist(file_path{i});
end
catch me
fprintf("Error opening file: %s\n", me.message());
% this message prompts: "File does not exist: ../dat/file1
% Error opening file: Invalid file identifier. Use
% fopen to generate a valid file identifier."
end

Answers (1)

dpb
dpb on 10 May 2022
if exist(file, "file")
will only find the file if it is in the current working directory or on the search path and only if the variable file (which isn't defined in the code we can see so we don't know what it is) is defined to contain a filename that is in one of those locations.
The syntax "../dat/file1" only works if you're currently in a folder on the same level in the hierarchy as dat; the two dots take you a level higher than currently are before looking below that level. We can't tell what cd would return at the time you tried the above.
In general, it's far better to use fully-qualified filenames built via the fullfile function to explicitly locate and subsequently open the file than to rely on partial paths; they lead to issues like the above where the result is wholly dependent upon the current working directory.
Establish a base root directory and then create all names explicitly from that location -- if you want/need for user to be able to use different working areas, then use uigetdir to set a default location for that session.
  3 Comments
Bojan Poletan
Bojan Poletan on 12 May 2022
Thanks, so if I want to build the path for feature_extraction.m, in the matlab terminal I type:
>> matlabroot
ans =
'/Applications/MATLAB_R2020a.app'
>> which feature_extraction.m
/Users/irene/Desktop/matlab_class/feature_extraction.m
I should write something like:
my_file_path = fullfile(matlabroot, "/Users/irene/Desktop/matlab_class/feature_extraction.m");
fid = fopen(my_file_path, "r");
Right ?

Sign in to comment.

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by