How to specify/change function input variable types

12 次查看(过去 30 天)
Hello,
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.
Thanks!

回答(3 个)

Walter Roberson
Walter Roberson 2011-11-25
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.

Cayleigh
Cayleigh 2011-11-25
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?

Cayleigh
Cayleigh 2011-11-25
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.

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by