Grey_Hunter
Grey_Hunter

Reputation: 15

I am trying to extract from text file data and add it into the audioread function Matlab

I am extracting some data "names" from a txt file and I am trying to use that name for the audioread() function in order to go into the Matlab location and open the corresponding audio signal WAV or mp3.

 fid = fopen('names.txt','rt');
           text= fscanf(fid,'%s',1);
           index = 0;
           text = fscanf(fid,'%s',1);
           while(strcmp(text,'.')~=1)            
            index = index+1;            
            storeD{index} = text;
            signal=strcat('''',storeD,'.wav''');% or mp3
            x=audioread(signal);
            text = fscanf(fid,'%s',1);
            end
           fclose(fid);

The problem now is that I am getting an error : Expected input to be a non-missing string scalar or character vector. for this line here x=audioread(signal);

Upvotes: 1

Views: 171

Answers (1)

MichaelTr7
MichaelTr7

Reputation: 4757

I prefer storing multiple audio signals in a structure since they can be variable length and don't fit nicely into one variable of the array. Also just adjusted some of the name parsings. I also assumed the text file would be configured as so:

names.txt:

Jacob
Shawn
Sydney
Maria
Joseph
.

If the files are in a subfolder names Music:

./Music/Jacob
./Music/Shawn
./Music/Sydney
./Music/Maria
./Music/Joseph
.

Reading Data Flow Chart

Depending on how the strings are formatted in the file this line may have to be edited:

signal = strcat(storeD,'.wav');% or mp3

MATLAB Script:

Signal_Number = 1;
Audio_Signals = struct('Audio_File',[]);

fid = fopen('names.txt','rt');

%Grabbing the first name to start%
index = 1;
text = fscanf(fid,'%s',1);

%Continue to loop%
while(strcmp(text,'.')~=1)   
    
storeD{index} = text;
index = index+1;            
signal = strcat(storeD,'.wav');% or mp3


Audio_Signals(Signal_Number).Audio_File = audioread(signal{1,Signal_Number});
Signal_Number = Signal_Number + 1;
text = fscanf(fid,'%s',1);
end
fclose(fid);

Fs = 44100;
Audio_1 = Audio_Signals(1).Audio_File;
Audio_2 = Audio_Signals(2).Audio_File;
Audio_3 = Audio_Signals(3).Audio_File;

%Playing audio 1%
sound(Audio_3,Fs);

storeD

Extension: Reading LAB and CSV Files Based on Text File Names

Contents of .LAB File

Alex\A23
John\B21
Nick\K36

Using MATLAB version: R2019b

Upvotes: 1

Related Questions