I have this code for the callback of my button which is tasked to solve and graph. But when I try it, the plot doesn't appear in the axes. What did I do wrong?
Here's the code:
function button_Callback(hObject, eventdata, handles)
global Yo K r t Y T Y2
% hObject handle to button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of button
Yo= str2double(get(handles.value_Yo, 'string'));
K= str2double(get(handles.value_K, 'string'));
r= str2double(get(handles.value_r, 'string'));
t= str2double(get(handles.value_t, 'string'));
Y= (K*Yo)/((K-Yo)*exp(-r*t)+Yo);
set(handles.ans, 'string', Y);
for T= 0:0.5:t;
Y= (K*Yo)/((K-Yo)*exp(-r*T)+Yo);
set(handles.axes1);
plot(T,repmat(Y,1,numel(T)),'o');
xlabel ('t(time)');
ylabel ('Y(population)')
end

 채택된 답변

Walter Roberson
Walter Roberson 2019년 4월 10일
편집: Walter Roberson 2019년 4월 10일

0 개 추천

The call
set(handles.axes1);
fetches the list of public properties that are available to be set for handles.axes1. The list of properties is then discarded because they are not assigned to anything and because you have a semi-colon at the end of the line to supress output.
In particular, the call does not make that axes the "active" axes, so the following plot() call will go to whichever axes happens to have last been activated.
Also, you do not have a "hold on", so every plot() call is erasing the previous plot.

댓글 수: 4

Via
Via 2019년 4월 10일
I only have one axes in my gui and there are changes in the axes but my problem is that the plot only appears as a point, not a curve. What code should I use?
Walter Roberson
Walter Roberson 2019년 4월 10일
As I wrote,
"Also, you do not have a "hold on", so every plot() call is erasing the previous plot."
Your path should seem clear: add a "hold on".
Via
Via 2019년 4월 10일
this is my current code now, and the graph already appeared but in circular points
for T= 0:0.5:50
Y= (K*Yo)/((K-Yo)*exp(-r*T)+Yo);
set(handles.axes1)
plot(T,repmat(Y,1,numel(T)),'-o');
hold on;
xlabel ('t(time)');
ylabel ('Y(population)')
end
but if I use
plot(T,repmat(Y,1,numel(T)),'-');
to change the circle marker into a line, the graph doesn't appear
Walter Roberson
Walter Roberson 2019년 4월 10일
Each time you call plot, it creates a new line() object that does not connect to any previous line objects (unless you matched the coordinates), including not automatically joining up to any coordinates you had plot() before. Furthermore, each line() object will only draw a visible line provided that it has at least two consecutive finite (and non-nan) coordinates. You are only plotting with single X coordinates (because T is a scalar inside the loop) so you cannot get lines.
You need to give up on getting lines as long as you are plotting one point at a time in a loop.
If you need to draw lines, then you should calculate the Y values and store them in a vector, without plotting them as you create them. Then once you have them all calculated, plot() the vector.
... If you use vectorization then you do not need any loop at all.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Line Plots에 대해 자세히 알아보기

질문:

Via
2019년 4월 10일

댓글:

2019년 4월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by