AppDesigner: Replace in all .ValueDisplayFormat € with $

11 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Adam Danz
Adam Danz il 8 Lug 2020
Modificato: Adam Danz il 27 Lug 2020
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 Commenti
Maria Hart
Maria Hart il 28 Lug 2020
Dear Adam,
thank you very much for your help! It worked :-)

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by