フィルターのクリア

Passing value of Edit Field box in app designer to external function/.m file

16 ビュー (過去 30 日間)
Shabnam Noor
Shabnam Noor 2023 年 9 月 22 日
回答済み: dpb 2023 年 9 月 25 日
I am trying to pass the value from the EditField box to a variable inside a .m file, upon the press of a button (code attached). I tried following: https://uk.mathworks.com/matlabcentral/answers/1848968-passing-variable-from-matlab-app-designer-edit-field-to-a-variable-inside-a-m-file?requestedDomain=. However, the value does not seem to show up in the command window or as a variable in the workspace. Any ideas?

回答 (2 件)

dpb
dpb 2023 年 9 月 23 日
"...pass the value from the EditField box to a variable inside a .m file, upon the press of a button"
Simply call the function in the button callback (either directly or from another function) with the desired value as the argument just as with any other MATLAB function call.
That's precisely what the accepted answer suggests and shows how to do.
NOTA BENE: the value returned from an edit field is a character string; if it is numeric and the function expects a number you've got to convert to numeric; there is a numeric edit field component if numbers are expected.
  4 件のコメント
Shabnam Noor
Shabnam Noor 2023 年 9 月 25 日
Thank you.
I have been able to write the value to a file and load it. However, when I turn knob A, the value of which is displayed by Edit Field, and then press the button, no value is saved. If I type the same value manually in Edit Field and then press the button, that value is saved. Is this because the callback that I am using for the knob is a 'value changing' one, rather than a 'value changed' one. The button is currently linked to Edit Field via a 'value changed' callback. If I want to save the value being chosen by turning the Knob and displayed by Edit Field, do I need to change the Knob callback to 'value changed'?
dpb
dpb 2023 年 9 月 25 日
That's the issue I noted above --
Also NOTA BENE -- the EditFieldValueChanged callback will also not be triggered by a programmatic change of the control .Value content--only the user action. See uieditfield <value changed callback>
So, as you have it structured, when the knob callback changes the .Value field of the numeric edit field, no callback is generated and thus you never see the changed value that relies on the call back function to update the app variable.
You need the knob valueChanged callback function to update the edit field value and also set the app variable to match since the action of changing the knob doesn't trigger a callback of the numeric edit value by its programmatic action. This could be either as an explicit assignment or by having the knob callback function subsequently call the edit field callback function itself.

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


dpb
dpb 2023 年 9 月 25 日
The above comment would be the klunky way to fix what you have presently by making the two callbacks work to do the same thing. You didn't describe the whole problem initially, so it wasn't clear what the actual problem really was which lead down that particular rabbit hole. There was nothing about a knob in the original posting.
<See the link> for example to have only the button have the callback function to update a plot using a user set number of contour levels from an edit field. You could restructure that way similarly although if you want to keep the ability of the user to directly enter a value besides using the knob then you need its callback to update the knob just as you need the knob callback to update the edit field.
The alternative there would be to only let the user interact with the knob and replace the numeric edit field with a text field that just displays the value but is not editable...that removes one interactive component although depending on the actual use, might be less convenient.

カテゴリ

Help Center および File ExchangeString についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by