AppDesigner: Replace in all .ValueDisplayFormat € with $

Dear All,
I am currently designing an App where one can select different locations. Depending on the location I would like to change in all numeric edit fields € to $. I know that I should use the command strrep(), however, I have several numeric edit fields, let's say I have (in reality I have many many more):
app.power.ValueDisplayFormat = '%.2f L';
app.valueperkW.ValueDisplayFormat = '%.2f €/L';
app.totalvalue:ValueDisplayFormat = '%.2f €';
Now, I would like to change all apps that contain .ValueDisplayFormat and have the € value in them.
% Value changed function: Standort
function StandortValueChanged(app, event)
switch app.standort.value
case 'Germany'
strrep()
case 'USA'
strrep()
end
end
A hint would be very much appreciated :)
Cheers

 採用された回答

Adam Danz
Adam Danz 2020 年 7 月 8 日
編集済み: Adam Danz 2020 年 7 月 27 日

0 投票

Here's a cleaner approach.
Step 1: Add a function to control the ValueDisplayFormat
Add the following function to your app [Instructions how to add a function in App Designer]. See comments for important details.
function controlValueDisplayPrefix(app, symbol)
% Symbol is a character such as $ or € or a character vectors such as
% char([1083,1074]) that defines the monetary prefix.
% 1) Search for objects that have a ValueDisplayFormat property.
% This will only search objects that are children of the main figure.
% If your numeric text objects are children of different objects such as
% panels, they will not appear here. Alternatively, you could just manually
% list the handles.
h = findall(app.UIFigure.Children, '-Property', 'ValueDisplayFormat');
% 2) Set format with updated symbol.
% You can rearrange the symbol placement as needed.
set(h, 'ValueDisplayFormat', [symbol,'%.2f'])
% Alternative:
% set(h, 'ValueDisplayFormat', [%.2f ',symbol])
end
Call this function from anywhere within your app using the following syntax
controlValueDisplayPrefix(app, '$');
controlValueDisplayPrefix(app, '€');
% etc...
Alternative function to replace symbols
This function also searches for all objects with ValueDisplayFormat properties but instead of assigning a standard format, it search for monetary symbols and replaces them. See inline comments for details.
function controlValueDisplayPrefix(app, symbol)
% Symbol is a character such as $ or € or a character vectors such as
% char([1083,1074]) that defines the monetary prefix.
% 1) Search for objects that have a ValueDisplayFormat property.
% This will only search objects that are children of the main figure.
% If your numeric text objects are children of different objects such as
% panels, they will not appear here. Alternatively, you could just manually
% list the handles.
h = findall(app.UIFigure.Children, '-Property', 'ValueDisplayFormat');
% 2) Replace any of the symbols listed in "possibleSyms"
% with the symbold defined in "symbol" input.
possibleSyms = {'$', '€', char([1083,1074]), char(165)}; % <--- LIST ALL POSSIBILITIES HERE !
vdf = get(h, 'ValueDisplayFormat');
vdfUpdated = regexprep(vdf, possibleSyms, symbol)
set(h,{'ValueDisplayFormat'},vdfUpdated)
end
Call the function using the same syntax,
controlValueDisplayPrefix(app, '$');
Step 2: Call the function with specific prefix symbols
Example:
function StandortValueChanged(app, event)
switch app.standort.value
case 'Germany'
controlValueDisplayPrefix(app, '€');
case 'USA'
controlValueDisplayPrefix(app, '$');
case 'Bulgaria'
controlValueDisplayPrefix(app, char([1083,1074])) % 'лв'
case 'Japan'
controlValueDisplayPrefix(app, char(165)); % '¥'
otherwise % <--- always include a default
controlValueDisplayPrefix(app, '??');
end
end
Tip: to avoid Upper/Lower case mismatch problems, set the swich value to lower case and set all cases to lower case.
switch lower(app.standort.value) % <--- lower()
case 'germany' % <--- all lower case
. . .
case 'usa' % <--- all lower case
. . .
% etc.....
end

6 件のコメント

Maria Hart
Maria Hart 2020 年 7 月 9 日
Dear Adam,
thank you very much for your suggestion. I have just tried it in my code. Using this, I encounter several problems:
Firstly, I now have in all EditFields the ValueDisplayFormat € (or $, etc.). However, I do have EditFields which , have the ValueDisplayFormat €/L, €/kW, etc. I am now missing the /L and /kW.
Secondly, I do have ValueDisplayFormats that do not correspond to %.2f, but to the default %11.4g or else.
Thirdly, I also have EditFields that do not have any ValueDisplayFormat with a monetary value such as qm, L, etc. pp..
With kind regards
Maria
Adam Danz
Adam Danz 2020 年 7 月 9 日
編集済み: Adam Danz 2020 年 7 月 13 日
These kinds of details should have been included in the question.
I've updated my answer to suggest a second controlValueDisplayPrefix function that should address these issues.
If you continue to have problems, please share what you've tried to do to fix them.
Maria Hart
Maria Hart 2020 年 7 月 26 日
編集済み: Maria Hart 2020 年 7 月 26 日
Dear Adam,
thank you very much for the update. I have tried the code, however, vdfUpdated does not seem to be assigned. I have tried to write a set command as in the example before, but this will return an error message.
function controlValueDisplayPrefix(app, symbol)
h = findall(app.Simulation.Children, '-Property', 'ValueDisplayFormat');
possibleSyms = {'MGA', '€', char(8363), char(3647)};
% MGA: Malagasy Ariary
% char(8363): Vietnamesischer Dong
% char(3647): Thai Baht
vdf = get(h, 'ValueDisplayFormat');
vdfUpdated = regexprep(vdf, possibleSyms, symbol);
set(vdfUpdated, ValueDisplayFormat');
end
I have further tried to insert vdfUpdated in the controlValueDisplayPrefix:
controlValueDisplayPrefix(app, vdfSymbol)
But this does not work as well.
With kind regards
Maria
Adam Danz
Adam Danz 2020 年 7 月 27 日
編集済み: Adam Danz 2020 年 7 月 28 日
I forgot the set() command. The code in my answer has been updated. I've added the line,
set(h,{'ValueDisplayFormat'},vdfUpdated)
Maria Hart
Maria Hart 2020 年 7 月 28 日
Dear Adam,
thank you very much for your help! It worked :-)
Adam Danz
Adam Danz 2020 年 7 月 28 日
Glad I could help!

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeApp Building についてさらに検索

質問済み:

2020 年 7 月 8 日

編集済み:

2020 年 7 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by