avoid inserting input as a string with ' '

7 Ansichten (letzte 30 Tage)
gabriele fadanelli
gabriele fadanelli am 7 Apr. 2020
Bearbeitet: dpb am 8 Apr. 2020
I have what shoud be a short and simple question in Matlab: let's say I have a function with some inputs: either a filename i.e.:
function DataSet = extractData(filename,.......)
[~,~,DataSet]=xlsread(filename)
end
or like a value I want to switch in order to let my function use different solutions, i.e.:
function result = operation(valuesSwitched,.......)
switch valuesSwitched
case one
result=1;
case two
result=2
...
end
end
and IN BOTH CASES I WISH TO AVOID calling the inputs as strings. Is there a possibility to do this, or in both cases inputs should be always represented as strings?
thanks
Gabriele
  5 Kommentare
Steven Lord
Steven Lord am 8 Apr. 2020
and the only way to declare it different is to cast it explicitly.
This is a bit of a tangent, but do you consider the "0x" and "0b" prefixes and the u{8, 16, 32, 64} and s{8, 16, 32, 64} suffixes introduced in release R2019b to be explicit casts?
>> x = 0xff
x =
uint8
255
>> y = 0xbadbeef
y =
uint32
195935983
>> z = 0b10011001
z =
uint8
153
>> w = 0b1001s32
w =
int32
9
dpb
dpb am 8 Apr. 2020
Bearbeitet: dpb am 8 Apr. 2020
Wasn't aware of them yet, Steven...thanks for pointing out the feature.
I'll have to mull it over some before making a rash judgement statement. :)
LATER:
OK, I think I would say it is the same as and ergo, yes, an explicit cast.
There's still no statement-like feature such as
INTEGER [KIND=scalar_int] variable_name
for Fortran to specify the type of integer for the name first; it's all implied that unless the special rule of the type prefix or suffix is appended comes to play that any variable will be double with no work necessary.
To me, while I applaud its introduction, the above are "syntactic sugar" that sweetens the pot and eases the amount of typing otherwise required to reach the same goal so in my mind the idea presented above still holds.
It is somewhat of a sidebar overall, granted, but the poster to whom I responded seemed think there really were two variable types there in whether was or was not an integral value.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

gabriele fadanelli
gabriele fadanelli am 8 Apr. 2020
very thankful. Now everything is clear to me
  1 Kommentar
dpb
dpb am 8 Apr. 2020
If the above answered the Q?, please Accept an Answer so folks can know...

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

dpb
dpb am 7 Apr. 2020
"So in case i want to call my input variable (valuesSwitched) in the second case one or two, I should ALWAYS use a string like 'one' or 'two'? No way to escape this?"
It all depends on the way you write the switch construct case statements. If you write
function result = operation(valuesSwitched,.......)
switch valuesSwitched
case one
result=1;
case two
result=2
...
end
end
then the two case comparisons are
valuesSwitched==one
valuesSwitched==two
but one and two are undefined variables in the function as shown. Instead you undoubtedly meant to write the functionality expressed as
function result = operation(valuesSwitched,.......)
switch valuesSwitched
case 1
result=1;
case 2
result=2
...
end
end
to compare against a numeric value of 1 or 2. If you really did want to use strings comparison instead, you'd have to write
function result = operation(valuesSwitched,.......)
switch valuesSwitched
case 'one'
result=1;
case 'two'
result=2
...
end
end
I don't understand at all the first lament of "IN BOTH CASES I WISH TO AVOID calling the inputs as strings." As pointed out above, the two functions are something entirely different -- in the first you need a file name at some point; may as well pass it to begin with unless there is some (small) set of possible files to select from and it's a choice of one of those you're after as a shortcut. If that's the case, if it's user input, use a uilistbox or uigetfile to just let the user select from the limited choices you'll give him.
  1 Kommentar
dpb
dpb am 8 Apr. 2020
ADDENDUM:
BTW,
function result = operation(valuesSwitched)
switch valuesSwitched
case {'one',1}
result=1;
case {'two',2}
result=2
end
end
would allow for either form of the argument to be passed...by make the case expression a cell array, the match is for any one of the cell content to satisfy the condition statement.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by