Problems accessing files from code after moving a matlab project from uni pc to home

1 view (last 30 days)
Bojan Poletan
Bojan Poletan on 21 May 2022
Commented: Bojan Poletan on 21 May 2022
I am having a problem when opening files in Matlab. I am working on a course project at uni and I should use a fully working code provided by teachers and make some practice with it.
Unfortunately, after installing matlab R2022a and fixed all dependencies, after running the file
feature_extraction.m
(as sait in the instructions) I faced the following error:
Error using textscan
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in addi_features_from_list (line 11)
file_path = textscan(fid, '%s', 'delimiter', '\n', 'whitespace', '');
Error in feature_extraction (line 33)
[Features, file_path] = addi_features_from_list(fname_in);
Here is the code giving the error:
function [Features, file_path] = addi_features_from_list(file)
fprintf('File: %s \n', file); % Output: File: dat/controlled_config_1_train.dat
fid = fopen(file, 'r');
file_path = textscan(fid, '%s', 'delimiter', '\n', 'whitespace', '');
file_path = file_path{1};
N = length(file_path);
fprintf('\nNumber of images: %d \n', N);
Features = zeros(N, 152);
for i = 1:N
[~,file_name,ext] = fileparts(file_path{i});
fprintf('process file: %s\n', [file_name ext]);
Features(i,:) = addi_features(file_path(i));
end
end
Here is the hirarchy of my working directory (the whole directory has been added to the path):
I also tried to see if the file exists from matlab but I always get a negative answer. Howcan i solve this?
Thanks.
PS: I am using MacOS (I also tried on Win 10)

Answers (1)

Jan
Jan on 21 May 2022
Do not call fopen without checking for errors:
[fid, msg] = fopen(file, 'r');
assert(fid > 0, 'Cannot open file for reading: %s', file);
In the commen you mention, that file is "dat/controlled_config_1_train.dat".
The screenshot shows, that such a file does not exist, but:
dat/controlled_config_3_train.dat
% ^
  1 Comment
Bojan Poletan
Bojan Poletan on 21 May 2022
You are right. But this is very strange... Probably the error is somewhere else. I will contact the teacher.

Sign in to comment.

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by