How to assign a different name of a matrix for each iteration?

10 ビュー (過去 30 日間)
Ana Bianco
Ana Bianco 2019 年 9 月 9 日
コメント済み: Ana Bianco 2019 年 9 月 9 日
Hi everyone,
I am trying to, for each iteration (that goes from 1 to 270), assign a different name for the result matrix of the function modalfit from Matlab Signal Processing Toolbox.
The matrix is a 1x10 for each iteration.
For example, I want for j=1 that the matrix is called [fn1] and saved in the workspace...
This is the code:
for j = 1 : size(varargout,2)
[fn]= modalfit(varargout{1,j}, f, fs, 10, 'FreqRange', [0 3000], 'FitMethod' , 'PP');
end
I would be very happy to be helped.
Thanks, Ana

採用された回答

Fabio Freschi
Fabio Freschi 2019 年 9 月 9 日
% preallocation
fn = cell(size(varargout,2),1);
for j = 1 : size(varargout,2)
fn{j} = modalfit(varargout{1,j}, f, fs, 10, 'FreqRange', [0 3000], 'FitMethod' , 'PP');
end
and you can get the desired matrix as fn{1}, fn{2}, etc.
  1 件のコメント
Ana Bianco
Ana Bianco 2019 年 9 月 9 日
That worked perfectly well, thanks Fabio!

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

その他の回答 (1 件)

Johannes Fischer
Johannes Fischer 2019 年 9 月 9 日
You could use assignin for that purpose:
for j = 1 : size(varargout,2)
fn = modalfit(varargout{1,j}, f, fs, 10, 'FreqRange', [0 3000], 'FitMethod' , 'PP');
assignin('base', ['fn' num2str(i)], fn)
end
but what speaks against storing it all in one 270x10 matrix?
fn = zeros(size(varargout,2), 10)
for j = 1 : size(varargout,2)
fn(j, :) = modalfit(varargout{1,j}, f, fs, 10, 'FreqRange', [0 3000], 'FitMethod' , 'PP');
end

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by