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

1 回表示 (過去 30 日間)
Maria Hart
Maria Hart 2020 年 7 月 22 日
回答済み: Roger J 2020 年 7 月 22 日
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 件のコメント
dpb
dpb 2020 年 7 月 22 日
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.

サインインしてコメントする。

採用された回答

Arthur Roué
Arthur Roué 2020 年 7 月 22 日
編集済み: Arthur Roué 2020 年 7 月 22 日
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]);

その他の回答 (1 件)

Roger J
Roger J 2020 年 7 月 22 日
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'

カテゴリ

Help Center および File ExchangeIntroduction to Installation and Licensing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by