Filter löschen
Filter löschen

Drawing Rectangles in GUI xy Plot - dimensions dependent on sliders

4 Ansichten (letzte 30 Tage)
Michela
Michela am 18 Jun. 2024
Beantwortet: Aman am 24 Jun. 2024
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 Kommentar
Umar
Umar am 18 Jun. 2024
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.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Aman
Aman am 24 Jun. 2024
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!

Kategorien

Mehr zu Develop Apps Using App Designer finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by