Why can't I import multiple files?

Hi, I'm building a simple speech recognition system. It works by asking for a voice input, and performing the appropriate functions on it. Then, I need it to open all the previously recorded samples, and compare it to them. That part is taken care of (more or less). I cannot seem to import the necessary files. Here is my code, with the error message, if you spot what is causing the problem please share it.

Answers (1)

Guillaume
Guillaume on 27 Mar 2018
Edited: Guillaume on 28 Mar 2018
'01.txt' and '1.txt' are two different filename. You ask to load the latter when it's the former that exists.
file = sprintf('%02d.txt', k);
would fix that issue.
However, I am not aware of an importdata that works on a datastore. So your code is going to fail on the next line. You seem to be mixing two completely different things, datastores and importdata. You need to use one or the other.
Finally, rather than hardcoding the count of files and the filenames, which innevitably you'll get wrong as you've just done, why not simply ask matlab what they are?
folder = 'C:/Users/..../...Recognition2'; %can't copy paste from a screenshot, you'll have to type the correct path
files = dir(fullfile(folder, '*.txt')); %get list of text files in the folder. Guaranteed to be correct with no typo!
mydata = cell(1, numel(files));
for fileidx = 1 : numel(files) %number of files guaranteed to be also correct.
mydata{fileidx} = importdata(fullfile(folder, files(fileidx).name));
%...
%Of course, doing mydata{fileidx} = y; after the previous line completely overwrite what has just been loaded
end

8 Comments

Ok, that problem has now been solved, but I now I simply get an error message saying unable to open file. Could the size of the files be the issue. There's 132300 double precision complex numbers in each one...
No, the file size is not the problem. Please post the relevant part of your code and a copy of the complete error message. The details matter. I assume you forgot to include the path of the file.

Looking at the filename that is generated would have immediately showed you what was wrong. I gave you a wrong format string. Correct one should be:

file = sprintf('%01d.txt', k);

On the other hand, I also gave you code that is guaranteed to generate the correct filenames whatever their name is since it simply asks the OS for their name. So whether it's '1.txt', '01.txt' or '001.txt' it'll load it.

@George: It would be much easier, if you post the text and code as text, not as screen shot. As I guessed, you forgot the path. dir(FolderName) imports the contents of the folder. You did not catch the output in a variable, such that the work is wasted. Much better:

Folder = 'C:\Users\...tooLazyToTypeThisFromYourScreenShot\'
...
  mydata{k} = imread(fullfile(Folder, file));

But Guillaume's approach is much better, because it does not depend on the numbering method. +1

Not enough input arguments now??
As said, it's easier if you copy/paste the error text and code rather than screenshot.
Not sure how that happened but that dir line was nonsense. I've fixed my answer.
@George: Guillaume's suggestion:
files = dir(fullfile(folder, '*.txt'))
Your code:
files = dir(fullfile, '*.txt')
Then the function fullfile does not get any inputs, exactly as the error message tells clearly.

Sign in to comment.

Asked:

on 27 Mar 2018

Commented:

Jan
on 28 Mar 2018

Community Treasure Hunt

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

Start Hunting!