How to automatize plot redraw for different initial data?

Hello,
I have a code drawing three different plots in one figure, consisting of three for loops, for some set initial data. The initial data is initialized at the beginning of the section. I would like to, for each time I hit the space key for example, redo the whole segment but change the initial data.
I was thinking about another for loop outside the inner threes but this feels like looking for trouble. I have been looking at functions that could do this but no luck.
My code looks like this:
initial values... d=20e-6
for th=0:1:s
i=eye(2)
for n=50:-1:1
i=i*function(n,th,initial data,d)
end
end
plot(th,I,'b')
hold on
next loop
So when this ends I'd like to press a key, and make d=19e-6 and redo the code, plot, until I'm good. Thanks for answers!

回答(1 个)

Just put this code in a function. You can then use guidata to store and load that initial value, and set it as the KeyPressFcn of your figure.

3 个评论

I'm not familiar using guidata and keypressfcn, could you please elaborate this answer?
Have you read the documentation for guidata or KeyPressFcn?
%Your function header should be like this:
function your_keypress_callback(source,eventdata)
handles=guidata(source);
d=handles.d;
%Your initializing function should contain something like this:
handles=struct('fig',gcf,'d',d);
guidata(handles.fig,handles)
set(handles.fig,'KeyPressFcn',@your_keypress_callback)
Preferably you would also include the handles to your subplot axes, so you can easily replace plots, instead of having to recreate the axes, but that is a next step.
Did my answer help you? If so, please mark it as accepted answer. If not, feel free to comment here with any remaining question.

请先登录,再进行评论。

类别

在 帮助中心 和 File Exchange 中查找有关 Creating, Deleting, and Querying Graphics Objects 的更多信息

标签

评论:

Rik
2018-2-19

Community Treasure Hunt

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

Start Hunting!

Translated by