Use Matlab Function Block to plot into figure created by Matlab Script

19 ビュー (過去 30 日間)
Jannis
Jannis 2022 年 11 月 6 日
コメント済み: Jannis 2022 年 11 月 15 日
Hello,
my plan is to create a figure with a Matlab script and then plot the results from my Simlink Simulation in this specific plot, using a Matlab function block.
Here is the script where the figure to plot the results is created:
ax1 = axes;
map_scales = imread("map_scaled.pgm");
imshow(map_scales, 'Parent',ax1)
hold on
My original idea was to use the ax1 in the matlab function block to plot the results in this figure. But this does not seem to work:
Here is a picture of the maltabl function block and the code inside it:
function path_plotting(x_pos, x_pos_prev, y_pos, y_pos_prev)
x_coodinates = [x_pos_prev, x_pos];
y_coodinates = [y_pos_prev, y_pos];
plot(x_coodinates, y_coodinates, 'LineWidth', 2, 'Color', 'b', 'Parent', ax1)
end
Does anyone have an idea how to solve this?

回答 (2 件)

Simon Chan
Simon Chan 2022 年 11 月 6 日
Try this in the function path_plotting
function path_plotting(x_pos, x_pos_prev, y_pos, y_pos_prev, ax1)
x_coodinates = [x_pos_prev, x_pos];
y_coodinates = [y_pos_prev, y_pos];
plot(ax1,x_coodinates, y_coodinates, 'LineWidth', 2, 'Color', 'b')
end
  1 件のコメント
Jannis
Jannis 2022 年 11 月 6 日
HI, thanks for you answer, but this does not seem to work. I think there might be no solution for my problem because simulink does not support the data type of an axes vairable.
I changed ax1 to a parameter data type and I received this error
See screenshot below

サインインしてコメントする。


Paul
Paul 2022 年 11 月 13 日
編集済み: Paul 2022 年 11 月 13 日
Hi Jannis,
I got this model to work
The code in the Matlab Function block is
function y = fcn(u,t)
persistent ax1
if isempty(ax1)
ax1 = gca;
set(ax1,'NextPlot','add');
end
plot(ax1,t,u,'r.')
y = u;
end
In your use case, maybe you can replace the ax1 = gca; with either your code, or a function that runs your code to create the image and return the axis handle. Anyway, I think that whatever you're trying to do is feasible.
  2 件のコメント
Jannis
Jannis 2022 年 11 月 14 日
Hi Paul,
thanks for your comment. I will try your recommendation and will let you know whether it works
Jannis
Jannis 2022 年 11 月 15 日
Thank you very much this solves my problem

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeSignal Attributes and Indexing についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by