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

1 visualizzazione (ultimi 30 giorni)
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 Commento
dpb
dpb il 22 Lug 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.

Accedi per commentare.

Risposta accettata

Arthur Roué
Arthur Roué il 22 Lug 2020
Modificato: Arthur Roué il 22 Lug 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]);

Più risposte (1)

Roger J
Roger J il 22 Lug 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'

Categorie

Scopri di più su Introduction to Installation and Licensing 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