把c语言转化为用matlab语言表达

2025-06-26 18:12:43
推荐回答(2个)
回答1:

function tiao_shu_feng_shan()
clear
speed = 100;
t = 0;
Y_a = 4;Y_b = 4;Y_c = 4;
y0 = figure('menubar','none');
axis equal;axis off
axis([-6 6 -10 6])
title('调速电风扇','fontsize',15);
grid off;
[x1,y1,z1]=sphere(30); %产生球体坐标
x=5*x1;y=5*y1;z=5*z1;
shading interp;
hold on;
mesh(x,y,z),colormap(hot); %画风扇框架
hold on;
hidden off;
hold on;
fill([-3,-1,1,3],[-8.5,-5,-5,-8.5],[0.5,0.5,0.5]); %画一个多边形
text(-1.7,-7.5,'星夜回缘 ','color','k','fontsize',15); %多边形里的文字
hold on
ax = Y_a * cos(2 * pi * t);ay = Y_a * sin(2 * pi * t); %计算初始三个叶片的横坐标和纵坐标
bx = Y_b * cos(2 * pi * t - 2 * pi/3);by = Y_b * sin(2 * pi * t - 2 * pi/3);
cx = Y_c * cos(2 * pi * t + 2 * pi/3);cy = Y_c * sin(2 * pi * t + 2 * pi/3);
y_line_a = line([0 ax],[0 ay],'EraseMode','xor','Color','r','linestyle','-','linewidth',20); %画出三个叶片
y_line_b = line([0 bx],[0 by],'EraseMode','xor','Color','b','linestyle','-','linewidth',20);
y_line_c = line([0 cx],[0 cy],'EraseMode','xor','Color','g','linestyle','-','linewidth',20);
k=1;

%b1为停止按钮
b1=uicontrol('parent',y0,...
'units','points',...
'tag','b2',...
'style','pushbutton',...
'string','停止',...
'backgroundcolor',[0.75 0.75 0.75],...
'position',[280 10 50 20],...
'callback','k=0;');

%b2为关闭按钮
b2=uicontrol('parent',y0,...
'units','points',...
'tag','b3',...
'style','pushbutton',...
'string','关闭',...
'backgroundcolor',[0.75 0.75 0.75],...
'position',[350 10 50 20],...
'callback',[...
'k=1;,',...
'close']);

%s1为调速框条
s1=uicontrol('parent',y0,...
'units','points',...
'tag','s1',...
'style','slider',...
'value',1*speed,...
'max',100,...
'min',30,...
'backgroundcolor',[0.75 0.75 0.75],...
'position',[30 10 190 20],...
'callback',[...
'm=get(gcbo,''value'');,',...
'speed = m/1;']);

%t1为上面的文字说明
% t1=uicontrol('parent',y0,...
% 'units','points',...
% 'tag','t',...
% 'style','text',...
% 'fontsize',15,...
% 'string','风速 请点击滑动条空白处',...
% 'backgroundcolor',[0.75 0.75 0.75],...
% 'position',[30 30 190 20]);

while 1 %让风扇转起来的循环
if k==0
break
end
t = t + 1/speed;
ax = Y_a * cos(2 * pi * t);ay = Y_a * sin(2 * pi * t);
bx = Y_b * cos(2 * pi * t - 2 * pi/3);by = Y_b * sin(2 * pi * t - 2 * pi/3);
cx = Y_c * cos(2 * pi * t + 2 * pi/3);cy = Y_c * sin(2 * pi * t + 2 * pi/3);
drawnow;
set(y_line_a,'XData',[0 ax],'YData',[0 ay]);
set(y_line_b,'XData',[0 bx],'YData',[0 by]);
set(y_line_c,'XData',[0 cx],'YData',[0 cy]);
end

然后使用MATLAB的Deployment Tool转换成c代码;

回答2:

基本上一样,有几点改动:
1)matlab要把n++写成n=n+1
2)printf在matlab中相近功能的命令有disp(),!echo,和sprintf(),其中最后一种最接近c中的printf。有关具体用法,你可以在matlab主命令窗口输入:help sprintf 来查阅这个命令的帮助和例子。比如:sprintf('The array is %dx%d.',2,3),其输出是 The array is 2x3
3)头文件的include是不用的,因为matlab的常用库里是包含了这些函数的。顺带一提,matlab里对函数的定义,开头要用function XXX。