Drawing Rectangles in GUI xy Plot - dimensions dependent on sliders

3 次查看(过去 30 天)
Hello everyone!
I am trying to model a 2D heat transfer process simplified as a 2 concentric rectangles for the cross sectional area. I wrote the code in Live Editor, and I am now trying to make an app.
This is my function in the GUI Code view:
function UIAxesButtonDown(app, event)
L_duct = 5 %in % https://www.mathworks.com/matlabcentral/answers/397383-how-to-draw-shapes-in-uiaxes-on-app-designer
rect1 = rectangle(app.UIAxes,'Position',[-L_duct/2 0 L_duct app.HEXLengthinSlider.Value])
rect2 = rectangle(app.UIAxes,'Position',[-app.HEXDiameterinSlider.Value/2 0 D_new app.HEXLengthinSlider])
end
However, nothing is showing on the plot when I click "Run" and change the dimensions.
  1 个评论
Umar
Umar 2024-6-18
When creating shapes in MATLAB App Designer, ensure that the plot updates correctly after each change. In the provided code snippet, there are a few potential issues that could be causing the rectangles not to display:
Make sure that the UIAxes is properly defined in the app and that the axes are correctly linked to the UI component.
Check if the function UIAxesButtonDown is triggered correctly upon interaction.
Verify that the variables D_new and app.HEXLengthinSlider are defined and have the expected values.
Ensure that the plot is being refreshed after updating the rectangle positions.
You can try adding a drawnow command after creating the rectangles to force the plot to update immediately.
Additionally, consider using hold on to retain previous plots while adding new ones.
By addressing these points, you should be able to visualize the rectangles in your 2D heat transfer model successfully.

请先登录,再进行评论。

回答(1 个)

Aman
Aman 2024-6-24
Hi Michela,
As per my understanding, you wanna draw rectangles once the axes is being clicked and are using slider values for calculating the position of the rectangle.
In the code that has been shared, there are two errors: first, the "D_new" variable definition is not present, and second, the handle "app.HEXLengthinSlider" is passed instead of a numeric value in the "Position" argument. Once this error is rectified, you will be able to see the rectangle once the axes is clicked. You can refer to the below code for reference, where I have rectified these errors:
function UIAxesButtonDown(app, event)
L_duct = 5 %in % https://www.mathworks.com/matlabcentral/answers/397383-how-to-draw-shapes-in-uiaxes-on-app-designer
D_new = 10;
rect1 = rectangle(app.UIAxes,'Position',[-L_duct/2 0 L_duct app.HEXLengthinSlider.Value]);
rect2 = rectangle(app.UIAxes,'Position',[-app.HEXDiameterinSlider.Value/2 0 D_new app.HEXLengthinSlider.Value]);
end
Refer to the below documentation to learn more about the "rectangle" function.
I hope this helps!

类别

Help CenterFile Exchange 中查找有关 Develop Apps Using App Designer 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by