variables in m files

Hi, I am new to MATLAB. This is the script i am working on:
FC = 360;
d = hex2dec ('*001C72*');
p = hex2dec ('*038E*');
t = hex2dec ('*0392*');
A = (d*0.5*FC/2^18);
B = (p*0.5*FC/2^15);
C = (t*0.5*FC/2^15);
Then writing 'A' in the command window i recieve an output in degrees depending on what the input 'd' is. My problem is that the hex values of d, p and t will change and not always equal the same. So is there any way that i can make the highlighted parts above a variable that i can somehow change without editing the m file everytime?
Many thanks

5 个评论

You could use a function
doc function
Could you provide an example using the code given in the OP?
Thanks very much for your answers. How would i implement 2s compliment in these fucntions?
i.e. in the current function if i enter hex value FE8D it would output decimal value 65186, however i would like the 2s compliment value -7282
Thanks in advance
José-Luis
José-Luis 2013-2-12
编辑:José-Luis 2013-2-12
That's a different question. Please accept an answer if it helped you and post a new question, but you should first try to see if it has been answered before.
Please look here for inspiration.
Thanks. The page you mentioned doesn't implement 2s compliment in the function. I'll keep searching for an answer although i have posted another question in the meantime. Thanks

请先登录,再进行评论。

 采纳的回答

function [A B C] = ImAFunction(d,p,t)
FC = 360;
d = hex2dec (d);
p = hex2dec (p);
t = hex2dec (t);
A = (d*0.5*FC/2^18);
B = (p*0.5*FC/2^15);
C = (t*0.5*FC/2^15);
Save it, place in the path or in the current folder and call it as follows:
[A B C] = ImAFunction(*001C72*', '*038E*', '*0392*');

更多回答(2 个)

Thorsten
Thorsten 2013-2-11
编辑:Thorsten 2013-2-11
Define function
function A B C = myfunction(d, p, t)
FC = 360;
A = (hex2dec(d)*0.5*FC/2^18);
B = (hex2dec(p)*0.5*FC/2^15);
C = (hex2dec(t)*0.5*FC/2^15);
Call function with values for d, p and t, e.g.,
[ A B C ] = myfunction('*001C72*', '*038E*', '*0392*');
Jan
Jan 2013-2-11
Str = '001C72';
d = hex2dec(Str);
Such basic methods are described in the Getting Started chapters of the documentation. To use a powerful tool like Matlab efficiently, it is strongly recommended to read the manual. The forum is not the right location to learn the basic, because the manual is written well and descriptive enough already.

类别

帮助中心File Exchange 中查找有关 Help and Support 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by