How do I create a mat file in a loop and store a matrix generated in that newly created mat file? (i also need to save every created mat file in the loop)

2 ビュー (過去 30 日間)
I tried with the following code but it says
for j=0:0
for k=0:1
filename=sprintf('%d%d.wav',j,k);
sin_of_sin=audioread(filename);
[st_matrix] = st_temp(sin_of_sin); % st_temp is my function file which returns the generated matrix
sprintf('angry_%d%d.mat',j,k)=st_matrix;
end
end
Error message:
Subscript indices must either be real positive integers or logicals.
  2 件のコメント
Jan
Jan 2018 年 3 月 24 日
Please post the complete error message, because it will tell us in which line the problem occurs.
sangeet sagar
sangeet sagar 2018 年 3 月 24 日
Subscript indices must either be real positive integers or logicals.
Error in stransform (line 14)
sprintf('angry_%d%d.mat',j,k)=st_matrix(:,1:50);

サインインしてコメントする。

採用された回答

Image Analyst
Image Analyst 2018 年 3 月 24 日
Instead of assigning a matrix to the sprintf() function, which of course you cannot do,
sprintf('angry_%d%d.mat',j,k)=st_matrix; % Bad syntax!!!
try this:
matFileName = fullfile(pwd, sprintf('angry_%d%d.mat',j,k));
save(matFilename, 'st_matrix');
  3 件のコメント
Image Analyst
Image Analyst 2018 年 3 月 24 日
It will make those files. Variables to save are listed in single quotes. Each file will have the current verions/copy of st_matrix stored inside of it. If you also want to save the filename itself inside of the mat file, you can add it:
save(matFilename, 'st_matrix', 'matFilename');
sangeet sagar
sangeet sagar 2018 年 3 月 24 日
Thank you.Worked like a charm

サインインしてコメントする。

その他の回答 (1 件)

Jan
Jan 2018 年 3 月 24 日
sprintf('angry_%d%d.mat',j,k)=st_matrix;
This cannot work. On the left you create a char vector, not a variable. In addition there is no need to create a variable dynamically only to create a MAT file. See also: Tutorial: Why to avoid to create variables dynamically.
If you want to create a MAT file, use save:
FileName = sprintf('angry_%d%d.mat',j,k);
save(FileName, 'st_matrix');
  2 件のコメント
sangeet sagar
sangeet sagar 2018 年 3 月 24 日
Can you please tell, why did you write 'st_matrix' here.
save(FileName, 'st_matrix');
I need to save the filename in every iteration. For example in first iteration- angry_00.mat, then in second iteration angry_01.mat and so on.
sangeet sagar
sangeet sagar 2018 年 3 月 24 日
Thank you Sir .Worked like a charm

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeAudio and Video Data についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by