Load files from relative path

function []= read_c3d_feat(output_list_relative)
% rather than fileread, importdata save each line separetely.
dir_list = importdata(output_list_relative);
dim_feat = 512;
for i = 1 : size(dir_list, 1)
dir_str = char(dir_list(i));
feat_files = dir([dir_str, '/*.res5b']);
num_feat = length(feat_files);
feat = zeros(num_feat, dim_feat);
for j = 1 : num_feat
feat_path = strcat(dir_str, '/', feat_files(j).name);
[~, feat(j,:)] = read_binary_blob(feat_path);
end
When i give input from command line like: read_c3d_feat('C:/Users/abc/Documents/MATLAB/features/0021.res5b')
Error using dir
Invalid path. The path must not contain a null character.
Error in read_c3d_feat (line 12)
feat_files = dir([dir_str, '/*.res5b']);
And When i give input from command line like: read_c3d_feat('C:/Users/abc/Documents/MATLAB/features')
Error using importdata (line 226)
Unable to load file.
Use TEXTSCAN or FREAD for more complex formats.
Error in read_c3d_feat (line 6)
dir_list = importdata(output_list_relative);
Caused by:
Error using fread
Invalid file identifier. Use fopen to generate a valid file identifier.

2 Comments

You question is unclear. In particular:
  1. your title asks about relative paths, but your question and examples only show absolute paths.
  2. you show examples where you call two different functions (once read_c3d_feat and once read_nvidia_c3d_feat), but you do not explain the difference, nor why you expect them to behave in the same way.
  3. The first error message explains clearly that there is an invalid character in the string. Please place this code exactly before that line, and show us what it displays when the error occurs:
+[dir_str, '/*.res5b']
Sorry Stephen Cobeldick, for my mistakes, however i have edit my question and i hope now it will give you more clarity in the question (about 1 and 2 point unclearness).
As you suggested in point 3 place the code "+[dir_str, '/*.res5b']" just before the line. I tried but its give again error like
read_c3d_feat('read_c3d_feat('C:/Users/abc/Documents/MATLAB/features/0021.res5b')
Struct contents reference from a non-struct array object.
Error in read_nvidia_c3d_feat (line 19)
feat_path = strcat(dir_str, '/', feat_files(j).name);

Sign in to comment.

 Accepted Answer

You have not indicated what format C:/Users/abc/Documents/MATLAB/features/0021.res5b is in.
I can tell from the first situation that the first thing in the file is being interpreted as numeric 0. char() of numeric 0 is called "the null character" and it is not permitted in file names.
In the second situation, the problem is that it is not permitted to importdata() a directory name.
I suggest changing the importdata to
dir_list = regexp( fileread( output_list_relative ), '\r?\n', 'split');
dir_list( cellfun( @isempty, dir_list ) ) = []; %remove empty lines

8 Comments

Dear Walter Roberson, Thank you for your swift response, as you mentioned that char() of numeric 0 is called "the null character" and it is not permitted in file names. However, i have tried without 0 but result is same(same error massage). Now my files are 21.res5b, 67.res5b, 45.res5b and so on.
Invalid path. The path must not contain a null character.
For second situation, as per your suggestion i have tried but it give me again difference error:
read_c3d_feat('C:/Users/abc/Documents/MATLAB/features')
Error using fileread (line 22)
Could not open file C:/Users/abc/Documents/MATLAB/features. Is a directory.
Error in read_c3d_feat (line 7)
dir_list = regexp( fileread( output_list_relative ), '\r?\n', 'split');
finish up the routine IN THE INDICATED PLACES
The routine will then take as input a file name. The file will be opened, and every line in it will be expected to contain the name of a directory. If there is some other delimiter between the directory names, such as "," or ":", then the regexp() will need to be slightly changed.
Each directory named in the file will then be examined for res5b files, each of which will be processed by read_binary_blob.
You need to finish off the code having to do with what you do with the blobs once you read them in.
function []= read_c3d_feat(output_list_relative)
dir_list = regexp( fileread( output_list_relative ), '[\r\n]+', 'split');
dir_list( cellfun( @isempty, dir_list ) ) = []; %remove empty lines
dim_feat = 512;
for i = 1 : size(dir_list, 1)
dir_str = dir_list{i};
feat_files = dir( fullfile(dir_str, '*.res5b') );
num_feat = length(feat_files);
feat = zeros(num_feat, dim_feat);
for j = 1 : num_feat
feat_path = fullfile(dir_str, feat_files(j).name);
[~, feat(j,:)] = read_binary_blob(feat_path);
end
FINISH YOUR "i" CODE HERE
end %of "for i"
DO SOMETHING WITH THE RESULTS HERE
end %of function
I have finished the code as per below. But still same error.
function []= read_c3d_feat(output_list_relative)
dir_list = regexp( fileread( output_list_relative ), '[\r\n]+', 'split');
dir_list( cellfun( @isempty, dir_list ) ) = []; %remove empty lines
dim_feat = 512;
for i = 1 : size(dir_list, 1)
dir_str = dir_list{i};
feat_files = dir( fullfile(dir_str, '*.res5b') );
num_feat = length(feat_files);
feat = zeros(num_feat, dim_feat);
for j = 1 : num_feat
feat_path = fullfile(dir_str, feat_files(j).name);
[~, feat(j,:)] = read_binary_blob(feat_path);
end
avg_feat = mean(feat, 1);
avg_feat_double = double(avg_feat);
fID = fopen(strcat(dir_str, '/c3d.res5b'), 'w');
% libsvm requires that input data must be double
fwrite(fID, avg_feat_double, 'double');
fclose(fID);
end
end
Error is: read_c3d_feat('C:/Users/abc/Documents/MATLAB/features')
Error using fileread (line 22) Could not open file C:/Users/abc/Documents/MATLAB/features. Is a directory.
Error in read_c3d_feat (line 2) dir_list = regexp( fileread( output_list_relative ), '[\r\n]+', 'split');
As I wrote,
"The routine will then take as input a file name. The file will be opened, and every line in it will be expected to contain the name of a directory."
You are passing in a directory, not the name of a file whose contents are to be read to determine the directories to examine.
Hello Walter Roberson,
I am quite new in the Matlab, I am not really understand what does mean by i am passing in a directory, not the name of a file whose contents are to be read to determine the directories to examine. I should pass the all the files name. as my files are 1.res5b, 67.res5b, 45.res5b and so on in the "features" folder. If you can clarify this i will appreciate your help.
Your initial code used importdata() and had the comment
% rather than fileread, importdata save each line separetely.
importdata() and fileread() are only for reading the content of a file. So the routine expects to be passed a file name, and the code after that in the routine deals with reading the content of the named file in order to find out what directories to pay attention to.
You need to decide whether you want to pass in the name of a file that lists the directories to work on, or if you instead want to pass in the name of a directory that all *.res5b files are to be processed in.
Sorry, I am going to bed now; I am falling asleep at the keyboard.
Thanks you so much Walter Roberson for your help. Actually i want to pass name of a directory that all *.res5b files are to be processed in. Anyway i will try to work on this, you take a good sleep.
function []= read_c3d_feat(dir_str)
feat_files = dir( fullfile(dir_str, '*.res5b') );
num_feat = length(feat_files);
feat = zeros(num_feat, dim_feat);
for j = 1 : num_feat
feat_path = fullfile(dir_str, feat_files(j).name);
[~, feat(j,:)] = read_binary_blob(feat_path);
end
avg_feat = mean(feat, 1);
avg_feat_double = double(avg_feat);
fID = fopen( fullfile(dir_str, 'c3d.res5b'), 'w');
% libsvm requires that input data must be double
fwrite(fID, avg_feat_double, 'double');
fclose(fID);
end
I would point out, however, that you are reading all of the *.res5b files in the directory and producing a *.res5b file in the same directory as output. That implies that if you were to run the code again on the same directory, that the c3d.res5b that you created last time would be read as input. Are you sure you want to write the output into the same directory, or are you should that you want it to be a .res5b file, or are you sure you do not want to ignore c3d.res5b when looking calculating the features ?

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!