0
votes

I am iterating through a directory and storing the full path of a file and its label in a cell matrix in Matlab this way:

 image_directory=dir(['path_for_directory/' char(folder1) '/' char(folder2)]);

            for j=3:size(image_directory,1)

                  myCellArray{index_image}{1}=['path_for_directory/' char(folder1) '/' char(folder2) '/' image_directory(j).name];
                  myCellArray{index_image}{2}=label;
                  index_image=index_image+1;
            end
       end
   end4

After I finish filling this cell matrix I want to save its contents (strings and integers) in a text file. I did something like the suggestion in (http://www.mathworks.com/matlabcentral/answers/112695-how-to-write-an-array-containing-letters-and-number-to-file). Here Is what I tried:

fid = fopen('data_out.txt','w');

for i=1:size(myCellArray,1)

    fprintf(fid, '%s \t %s\n', myCellArray{i,1}, sprintf('%d ', myCellArray{i,2}));
end

fclose(fid); 

The problem is, when I execute this source code in matlab I have the following error:

Error using sprintf
Function is not defined for 'cell' inputs.

Error in write_files (line 50)
            fprintf(fid, '%s \t %s\n', myCellArray{i,1}, sprintf('%d', myCellArray{i,2}));

What Is wrong with my code?

2
is myCellArray{i,2} a number?SamuelNLP
Yes, it is an integer number representing the label of the image.mad

2 Answers

0
votes

If myCellArray{i,2} is just one integer, I think you can just use,

fprintf(fid, '%s \t %d\n', myCellArray{i,1}, myCellArray{i,2});
0
votes

To solve the problem I just changed

image_directory=dir(['path_for_directory/' char(folder1) '/' char(folder2)]);

        for j=3:size(image_directory,1)

              myCellArray{index_image}{1}=['path_for_directory/' char(folder1) '/' char(folder2) '/' image_directory(j).name];
              myCellArray{index_image}{2}=label;
              index_image=index_image+1;
        end
   end
end

with:

image_directory=dir(['path_for_directory/' char(folder1) '/' char(folder2)]);

        for j=3:size(image_directory,1)

              myCellArray{index_image,1}=['path_for_directory/' char(folder1) '/' char(folder2) '/' image_directory(j).name];
              myCellArray{index_image,2}=label;
              index_image=index_image+1;
        end
   end

end

In other words, There were typos in the code that fills the cell matrix.