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

1 visualización (últimos 30 días)
Maria Hart
Maria Hart el 22 de Jul. de 2020
Respondida: Roger J el 22 de Jul. de 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 comentario
dpb
dpb el 22 de Jul. de 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.

Iniciar sesión para comentar.

Respuesta aceptada

Arthur Roué
Arthur Roué el 22 de Jul. de 2020
Editada: Arthur Roué el 22 de Jul. de 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]);

Más respuestas (1)

Roger J
Roger J el 22 de Jul. de 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'

Categorías

Más información sobre Introduction to Installation and Licensing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by