How do you save variables to vectors and plot them?
Afficher commentaires plus anciens
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 commentaires
madhan ravi
le 14 Mar 2019
dipidi dapudi doo , totally vague
madhan ravi
le 14 Mar 2019
post your code or upload your script file
Macy Walters
le 14 Mar 2019
Modifié(e) : madhan ravi
le 14 Mar 2019
Réponses (2)
KSSV
le 14 Mar 2019
x = zeros([],1) ;
y = zeros([],1) ;
for i = 1:10
x(i) = i ;
y(i) = rand ;
end
plot(x,y) ;
2 commentaires
Macy Walters
le 14 Mar 2019
KSSV
le 14 Mar 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)
Catégories
En savoir plus sur Sparse Matrices dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!