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')

Respuestas (1)

James Tursa
James Tursa el 20 de Mzo. de 2017

0 votos

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?

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 20 de Mzo. de 2017

Respondida:

el 20 de Mzo. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by