Converting NumericEditFields to TextEditFields in App Designer

14 views (last 30 days)
Is there an easy way to convert NumericEditField components to TextEditField components in Appdesigner? Unfortunately, "Replace with" does not give that as an option. I have 42 of them that I would like to convert [to TextEditField] so that users can type in executable commands instead of having to type numeric values. As one can imagine, creating "new" TextEditFields" to replace numeric ones would be a nightmare. Thank you in advance [even if the short answer is "no"]...

Answers (1)

Namnendra
Namnendra on 16 May 2024
Hi Gokhan,
There isn't a direct "Replace with" option in MATLAB App Designer for converting `NumericEditField` components to `TextEditField` components. This limitation means you can't simply right-click a component and replace it with a different type while retaining its properties (e.g., position, size).
However, I can offer a workaround that, while not entirely effortless, can significantly streamline the process, especially for a large number of components like your 42 `NumericEditField`s. This approach involves manually editing the `.mlapp` file, which is essentially a JSON-like structure under the hood.
Backup First!
Before proceeding, make a backup of your `.mlapp` file. Manually editing it can corrupt the file if not done carefully.
Manual Editing Steps
1. Open the `.mlapp` File: Navigate to your App Designer `.mlapp` file in MATLAB, right-click it, and choose "Open Outside MATLAB" or simply open it in a text editor (like Notepad++, Sublime Text, or Visual Studio Code).
2. Find and Replace: Use the editor's find-and-replace feature to replace instances that define a `NumericEditField` with a `TextEditField`. Specifically, you'll be looking to replace something like "Type":"matlab.ui.control.NumericEditField" with "Type":"matlab.ui.control.EditField".
3. Adjust Properties: Since you're changing from a numeric input to a text input, you might want to remove or edit any properties specific to `NumericEditField` that don't apply to `TextEditField`, such as `Limits`, `ValueDisplayFormat`, etc. This step might require a bit of trial and error to ensure compatibility.
4. Save and Reopen in App Designer: After making the changes, save your file and reopen it in App Designer. Check to ensure all components have converted properly and that no errors occur.
Caveats
- Risk of Corruption: Manually editing the `.mlapp` file can corrupt it if not done correctly. Always keep a backup.
- Version Compatibility: Future versions of MATLAB might change how App Designer stores component information, potentially making this workaround obsolete or requiring adjustments.
- Property Mismatch: Some properties relevant to `NumericEditField` won't apply to `TextEditField` and vice versa. You might need to manually adjust these in App Designer after conversion.
Alternative: Scripted Component Generation
If manual file editing seems too risky or cumbersome, another approach is to write a MATLAB script that programmatically generates `TextEditField` components based on the properties (position, size, etc.) of your existing `NumericEditField` components. Afterward, you can delete the original numeric fields. This approach gives you more control and can be automated, but it requires scripting knowledge and understanding of App Designer's programmatic component manipulation API.
Conclusion
Unfortunately, there's no built-in, one-click solution in App Designer for converting component types. The manual `.mlapp` file editing method is a workaround that can save time over manually deleting and recreating components but comes with its own risks and limitations. Always back up your work before attempting such modifications.
I hope the above information helps you.
Thank you.
  1 Comment
Gokhan Pekcan
Gokhan Pekcan on 16 May 2024
Hello Namnendra
I appreciate the trouble to explain your suggestion. Unfortunately, the code generated by the App Designer (.mlapp file) cannot be edited outside the App Designer's [MATLAB] editor.
It is possible to export .mlapp to .m to do (find/replace) just as you describe (which I did before succesfully), but then the .m file cannot be opened in [loaded back to] App Designer. Therefore, any additional/future editing in App Designer is not possible.
I believe I am going to have to wait until Mathworks decides to allow users to edit the "shaded" code in App Designer's editor.
Thank you,
Gokhan

Sign in to comment.

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!