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

 Accepted Answer

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 Comments

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
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.
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
I forgot the set() command. The code in my answer has been updated. I've added the line,
set(h,{'ValueDisplayFormat'},vdfUpdated)
Dear Adam,
thank you very much for your help! It worked :-)

Sign in to comment.

More Answers (0)

Categories

Find more on App Building in Help Center and File Exchange

Asked:

on 8 Jul 2020

Edited:

on 28 Jul 2020

Community Treasure Hunt

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

Start Hunting!