very thankful. Now everything is clear to me

6 views (last 30 days)

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

dpb
on 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.

dpb
on 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.

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 5 Comments

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/516185-avoid-inserting-input-as-a-string-with#comment_823342

⋮## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/516185-avoid-inserting-input-as-a-string-with#comment_823342

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/516185-avoid-inserting-input-as-a-string-with#comment_823344

⋮## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/516185-avoid-inserting-input-as-a-string-with#comment_823344

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/516185-avoid-inserting-input-as-a-string-with#comment_823359

⋮## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/516185-avoid-inserting-input-as-a-string-with#comment_823359

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/516185-avoid-inserting-input-as-a-string-with#comment_823848

⋮## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/516185-avoid-inserting-input-as-a-string-with#comment_823848

## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/516185-avoid-inserting-input-as-a-string-with#comment_823896

⋮## Direct link to this comment

https://fr.mathworks.com/matlabcentral/answers/516185-avoid-inserting-input-as-a-string-with#comment_823896

Sign in to comment.