How to call files with location names of >1 words?

1 vue (au cours des 30 derniers jours)
Maria Hart
Maria Hart le 22 Juil 2020
Réponse apportée : Roger J le 22 Juil 2020
Dear all,
I am writing an App with AppDesigner. In my Interface, I have several locations that can be chosen, namely Berlin, Amsterdam and Mexico City. In my folder, I have files which are called depending on the location. These files are called temperature_berlin, temperature_amsterdam and temperature_mexicocity. I do have implemented the lower() code, so my programm looks for lower case names. However, I do have the problem that Mexico City is written in two words, so my program does not recognize the according file.
location = lower(app.location.Value);
Struct_temperature = load(['.\data\temperatures\temperature_', location]);
temperature = Struct_temperature.(['temperature_',location]);
The file names are temperature_mexicocity etc.
Writing a file with - such as temperature_mexico-city is not allowed. This would also mean to write the location name in my interface as Mexico-City which is not very pretty.
Could somebody give me a hint?
With kind regards
Gudrun
  1 commentaire
dpb
dpb le 22 Juil 2020
Build a lookup table of the user name and the associated file names is the quick and dirty way...the somewhat cleaner but takes a little more code is to write a set of functions that translate to/from the actual user city name to the associated file name.
You can of course, even use "Mexico City Temperature.dat" as a file name if you make sure your app always quotes it. I don't recommend just for ease of coding to do so, but the OS will allow it.

Connectez-vous pour commenter.

Réponse acceptée

Arthur Roué
Arthur Roué le 22 Juil 2020
Modifié(e) : Arthur Roué le 22 Juil 2020
You can delete all spaces with the command
strrep(str, ' ', '')
Then
location = lower(strrep(app.location.Value, ' ', ''));
Struct_temperature = load(['.\data\temperatures\temperature_', location]);
temperature = Struct_temperature.(['temperature_',location]);

Plus de réponses (1)

Roger J
Roger J le 22 Juil 2020
You can use the following:
>> location = lower(fname); % convert to lower case
>> location = char(location); % convert to char array (rather than string)
>> location = location(location~=' ') % remove spaces if any exist
>> location = location(location~='-') % remove hyphens if any exist (as in Winston-Salem)
location =
'mexicocity'

Catégories

En savoir plus sur Introduction to Installation and Licensing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by