MATLAB Answers


How to specify/change function input variable types

Asked by Cayleigh
on 25 Nov 2011


I'd like to add an input, t, to a function, which needs to be type 'double'. Even if I've already defined t as a double, when it gets used as the function input, the function automatically assumes it is type 'char'. My understanding is that the function determines the variable type based on how it is used within the function, and this variable is only used to access a value from an array. The first time it is called, it's something like PV(t,4). Basically I want to run the function several times with different values of t in order to access different parts of an array each time. The function's only other input is a filename.

So, is there another way I can call it that would cause the function to see it as a 'double'? Or, is there a way to change it from 'char' to 'double' within the function? When it sees t as 'char', it is not getting the right value from the array.




No products are associated with this question.

3 Answers

Answer by Walter Roberson
on 25 Nov 2011

You almost certainly have a mismatch in the calling sequence between how it is being called and how it expects to be called.

Please carefully compare the call you are making against the "function" declaration for the function. You can put a breakpoint in at the first line of the routine, and if necessary, compare class() of each variable declared in the "function" line against the corresponding variable you pass in, and compare that again to the type your code treats each variable as being.

You indicate that "the function's only other input is a filename", but you show us a call of PV(t,4) and you tell us that t is a double, so your sample code does not show any character being passed in at all.


Answer by Cayleigh
on 25 Nov 2011

Thanks for the quick reply. I'm not completely sure I follow (I'm relatively new to Matlab). If I put a breakpoint after the very first line of the function, class(t) is already 'char'. So, as soon as it gets passed into the function it has already changed to 'char', before it is even called. This is a problem, I really need it to stay as a number. Otherwise, when I call it in PV(t,4), it does not work. Do you know why it might be converting it to 'char' as soon as it is passed into the function?


Answer by Cayleigh
on 25 Nov 2011

Alright, I got it figured out. I was calling my function like this:

power_flow 17bus.txt 13

When I guess I needed to be calling it like this:

power_flow('17bus.txt', 13)

Now it remains as class 'double'. Thanks for the help.

  1 Comment

That would do it alright!

Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!