How do you save variables to vectors and plot them?
Mostrar comentarios más antiguos
This is my code:
valueOfP = [];
valueOfD = [];
valueOfT = [];
valueOfH = [];
Irrelevant code that functions on its own
end
fprintf(' Altitude is %d Temp = %d Pressure = %d Density = %d \n', h, t, p, d)
end
valueOfP(h) = p;
valueOfD(h) = d;
valueOfT(h) = t;
valueOfH(h) = h;
plot(valueOfP,valueOfH)
plot(valueOfT,valueOfH)
plot(valueOfD,valueOfH)
I keep getting error messages when I try to run the code. How would I go about saving the variables and then plotting pvs h, t vs h, and d vs h?
3 comentarios
madhan ravi
el 14 de Mzo. de 2019
dipidi dapudi doo , totally vague
madhan ravi
el 14 de Mzo. de 2019
post your code or upload your script file
Macy Walters
el 14 de Mzo. de 2019
Editada: madhan ravi
el 14 de Mzo. de 2019
Respuestas (2)
KSSV
el 14 de Mzo. de 2019
x = zeros([],1) ;
y = zeros([],1) ;
for i = 1:10
x(i) = i ;
y(i) = rand ;
end
plot(x,y) ;
2 comentarios
Macy Walters
el 14 de Mzo. de 2019
KSSV
el 14 de Mzo. de 2019
You should specify the error.....how you expect us to correct it without code and showing us the error.
You define h to have values that are NOT positive integer:
for h=0:.5:105
which means that you cannot use h as an index. The simplest solution is to define a vector of h values, and loop over its indices. For your code you will need to do something like this:
hvec = 0:.5:105; % vector of h values
N = numel(hvec);
Pvec = nan(1,N); % preallocate output
Dvec = nan(1,N); % preallocate output
Tvec = nan(1,N); % preallocate output
for k = 1:N % loop over indices
h = hvec(k);
... your code
Pvec(k) = p;
Dvec(k) = d;
Tvec(k) = t;
end
plot(hvec,Pvec)
figure()
plot(hvec,Dvec)
figure()
plot(hvec,Tvec)
Categorías
Más información sobre Sparse Matrices en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!