Saving a 2D matrix each loop iteration as a specific file type and name

3 views (last 30 days)
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

Accepted Answer

Image Analyst
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
Chris Matthews
Chris Matthews on 27 Jan 2018
Thanks for the input - I had actually tried various versions of xlswrite, dlmwrite, save etc, but had removed them from my code for readability.
When using the second option, the results I'm after appear as RMS values in the first row of the excel spreadsheet, starting at A1 and continuing along B1, C1, D1 etc, and the original wavdata appears in the first column starting at A2, then A3, A4 A5 down the column.
This fits the purpose, so thanks heaps!!!!
On another note, you sound experienced, in your opinion, is there a preference to saving as either .mat file or .xls file?
I'm going to be calling the data back in later code for stat analysis.
Thanks again.
Image Analyst
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.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!