MATLAB Answers

AppDesigner: Replace in all .ValueDisplayFormat € with $

27 views (last 30 days)
Maria Hart
Maria Hart on 8 Jul 2020
Edited: Adam Danz on 28 Jul 2020 at 11:07
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

  0 Comments

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 8 Jul 2020
Edited: Adam Danz on 27 Jul 2020 at 12:59
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

Show 3 older comments
Adam Danz
Adam Danz on 27 Jul 2020 at 13:00
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 on 28 Jul 2020 at 9:06
Dear Adam,
thank you very much for your help! It worked :-)

Sign in to comment.

More Answers (0)