i'm trying to take the output from an if statement inside a for loop in an array but the values are getting overwritten here is my code
% clc
clear
n=input('enter number of variables: ');
r=input('please enter the limit: ');
z = cell(1,n);
y = zeros(1,n);
f = zeros(1,n);
for i=1:n
z{i}=input('enter the variable name: ','s');
y(i)=input('assign a number to this variable: ');
f(i)=input('second variable: ');
end
[ys,j]=sort(y,'descend');
fs=f(j);
zs=z(j);
k=n;
for l=1:n
if fs(l)>r
disp('This mud type can not be used')
disp(zs(l))
fx=length(fs(l));
zn=cell(1,fx);
zn(l)=zs(1);
end
end
input('end')

Antworten (1)

James Tursa
James Tursa am 20 Mär. 2017

0 Stimmen

These statements:
zn=cell(1,fx);
zn(l)=zs(1);
The first statement above wipes out any previous values/cells you had assigned to the variable zn and replaces it the a cell array containing empty cells. So you need to rewrite this code. What exactly do you want saved each iteration?

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 20 Mär. 2017

Beantwortet:

am 20 Mär. 2017

Community Treasure Hunt

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

Start Hunting!

Translated by