How do you save variables to vectors and plot them?
Show older comments
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 Comments
madhan ravi
on 14 Mar 2019
dipidi dapudi doo , totally vague
madhan ravi
on 14 Mar 2019
post your code or upload your script file
Macy Walters
on 14 Mar 2019
Edited: madhan ravi
on 14 Mar 2019
Answers (2)
KSSV
on 14 Mar 2019
x = zeros([],1) ;
y = zeros([],1) ;
for i = 1:10
x(i) = i ;
y(i) = rand ;
end
plot(x,y) ;
2 Comments
Macy Walters
on 14 Mar 2019
KSSV
on 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)
Categories
Find more on Sparse Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!