Saving a 2D matrix each loop iteration as a specific file type and name
3 views (last 30 days)
Show older comments
Chris Matthews
on 27 Jan 2018
Commented: Image Analyst
on 27 Jan 2018
Hi All, I've had a look at the other answers in the community and none seem to work for me, so here is my problem. In a for-loop, I'm uploading a series of audio files one at a time, reading it as wav data, performing calculations on it (RMS in this case) and then want to save both the y matrix (varying size, 2D) and the RMS array (varying length) each loop iteration, with a specific naming convention.
My code is below:
%Load files
%Define working folder
myfolder = 'H:\ERP2018\Actor 01 Normal Intensity Only;
myDir = 'H:\ERP2018\Actor 01 Normal Intensity Only; %gets directory
myFiles = dir(fullfile(myDir,'*.wav')); %table of file names, size, etc
%forloop
for k = 1:length(myFiles)
baseFileName = myFiles(k).name;
FileName=[baseFileName,num2str(baseFileName)] ;
fullFileName = fullfile(myfolder, baseFileName);
[wavData, Fs] = audioread(fullFileName);
y=wavData; %want to save this each loop through
%split into frames and save as 2D matrix
FrameLen=512;
%gives a frame length of 10.69ms approx
FramesMat= buffer(y, FrameLen);
FramesMat = FramesMat';
%gives matrix of 512 samples by a varying number of frames (about 227 frames)
RMSFramesMat = rms (FramesMat); %want to save this each loop through
end
In each loop, I want to save the y matrix, named as 'originalactor01-k' where k is an incremental as the loop runs, and want to save the RMSFramesMat as an array named 'RMSFramesMatactor01-k'
Ideally, as an excel readable file, unless there are better options, as this will be the basis of a stat analysis for my research. Any help that could be given would be great!
Thanks! Chris
0 Comments
Accepted Answer
Image Analyst
on 27 Jan 2018
How about using save() to save them in a .mat file? Otherwise simply use xlswrite(). Not sure where your difficulty in calling either of those functions lies. Doesn't even look like you tried to call either of them for some reason.
fullMatFileName = fullfile(myfolder, [baseFileName, '.mat']);
save(fullMatFileName, 'RMSFramesMat', 'y');
or something like
fullExcelFileName = fullfile(myfolder, [baseFileName, '.xlsx']);
xlswrite(fullExcelFileName, RMSFramesMat, 'Results', 'A1');
xlswrite(fullExcelFileName, y, 'Results', 'A2');
2 Comments
Image Analyst
on 27 Jan 2018
If your usage of them later is going to be in MATLAB and no other program, then use a .mat file. If you have some other program that needs to consume the data, then chances are it's not going to recognize the .mat format and then you should choose a format that that program can import, like an Excel workbook or a text file.
More Answers (0)
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!