How to save multiple outputs of a function with changing inputs

조회 수: 2 (최근 30일)
Matthew Williams
Matthew Williams 2018년 4월 18일
답변: Bob Thompson 2018년 4월 18일
G'day, I am fairly new to MATLAB and I am trying to run a filter function for two separate inputs but saving the output data from each one so that I can plot them on the same graph. I know I can just write the function again with a different input and a different output variable name but I want it so that if I had more than two I wouldn't have to write it out a whole bunch of times. The changing input is N1 = [6,3] and I would like to save P6 and Hertz6. Any help would be greatly appreciated, Thanks.
The code I have written so far:
Rp = 0.5;
Rs = 20;
fs2 = 10000;
fc2 = [500,1000];
Ts2 = 1/fs2;
fn2 = fc2/(fs2/2);
for N1 = [6,3]
[Y6,X6] = ellip(N1,Rp,Rs,fn2,'stop');
[P6,W6] = freqz(Y6,X6,100);
Hertz6 = W6/(2*pi*Ts2);
end
figure(3);
plot(Hertz6,abs(P6));
title('Band-Stop Filter (Remove frequency from 500Hz to 1kHz)');
axis([0 4000 0 4.5]);
hold on

채택된 답변

Bob Thompson
Bob Thompson 2018년 4월 18일

You want to store the data somewhere else inside your for loop.

for N1 = [6,3]
 ...
 dataout(1) = P6; % Store P6 into first element of dataout
 dataout(2) = Hertz6; % Store Hertz6 in second element
end

If your data is not a single value you can store to a range, dataout(:,1), or to a cell, dataout{1}.

Alternatively, you can just index P6 and Hertz6 for each loop of the for loop.

counter = 0;
for N1 = [6,3]
 counter = counter +1;
 ...
 [P6(:,counter),W6] = ... % Store P6 results in column 'counter'
 Hertz6(:,counter) = ... % Store Hertz6 results in column 'counter'
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by