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 CenterFile Exchange에서 Introduction 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