Creating a C from from a Script

1 view (last 30 days)
Jucimar Carpe
Jucimar Carpe on 15 Nov 2018
Answered: Fred Smith on 20 Nov 2018
Hi folks!
I want to create a standalone application to run a script that I've wrote in matlab script. And I know that i need to create a GUI for that as well, but to integrate the GUI and my script i need to compile to C first. The point is, as far i know i need to create first a function to call my script, the problem is that my program has many functions inside of it, so how can generate a function such like that?
Here is my code. The user will need to insert the first 6 parameters in the future application: S,V,X0,X1,X2,Zaterramento
%Este Script foi desenvolvido para resolver CC bifásico para terra em geradores
%Curto aplicado as fases B e C
%Desenvolvimento: Jucimar Carpe
%Data:13/11/2018
%Matéria de Aplicação: Sistemas Elétricos de Potência
%
clear all
close all
%
% Basta informar os 6 parâmetros abaixo do gerador;
S=30e6;
V=13.8e3;
X0=0.08;
X1=0.2;
X2=0.25;
Zaterramento_ohms=0.571320;
%Deste Ponto em diante o programa calculará todo o restante
Zbase=((V.^2)/S);
Zn=3*(Zaterramento_ohms/Zbase);
Zparalelo_X2_X0=((j*(X0+Zn)*j*X2)/(j*(X0+Zn)+j*X2));
Ztotal=Zparalelo_X2_X0+(j*X1);
Ibase=(S/(sqrt(3)*V));
Ia1=j/Ztotal;
%
%Aplicar divisor de corrente para descobrir I2 e I0
%
Ia2=(-Ia1*j*(X0+Zn))/(j*(X2+X0+Zn));
Ia0=(-Ia1*j*(X2))/(j*(X2+X0+Zn));
%
%Defasagem na corrente para os vetores das fases B e C
a=((-1/2)+j*(sqrt(3)/2));
a_quadrado=((-1/2)-j*(sqrt(3)/2));
%
Ib0=Ia0;
Ib1=Ia1*((-1/2)-j*(sqrt(3)/2));
Ib2=Ia2*((-1/2)+1i*(sqrt(3)/2));
Ic0=Ia0;
Ic1=Ia1*((-1/2)+j*(sqrt(3)/2));
Ic2=Ia2*((-1/2)-j*(sqrt(3)/2));
%
Ia0_A=Ibase*Ia0;
Ia1_A=Ibase*Ia1;
Ia2_A=Ibase*Ia2;
Ib0_B=Ibase*Ib0;
Ib1_B=Ibase*Ib1;
Ib2_B=Ibase*Ib2;
Ic0_C=Ibase*Ic0;
Ic1_C=Ibase*Ic1;
Ic2_C=Ibase*Ic2;
%
%Montagem da matriz TDI
TDI=[1 1 1; 1 a_quadrado a; 1 a a_quadrado];
%
Matriz_Ia0_Ia1_Ia2=[Ia0; Ia1; Ia2];
Ia_Ib_Ic_pu=TDI*Matriz_Ia0_Ia1_Ia2;
IA=Ibase*Ia_Ib_Ic_pu(1,1);
IB=Ibase*Ia_Ib_Ic_pu(2,1);
IC=Ibase*Ia_Ib_Ic_pu(3,1);
Icc_pu=3*Ia0;
Icc=Ibase*Icc_pu;
Va0=-1*Ia2*j*X2
Va1=-1*Ia2*j*X2
Va2=-1*Ia2*j*X2
Va0_Va1_Va2=[Va0; Va1; Va2];
Va_Vb_Vc_pu=TDI*Va0_Va1_Va2
%
%Tensões de fase do gerador
%
VA_fase=(V/(sqrt(3)))*Va_Vb_Vc_pu(1,1)
VB_fase=(V/(sqrt(3)))*Va_Vb_Vc_pu(2,1)
VC_fase=(V/(sqrt(3)))*Va_Vb_Vc_pu(3,1)
%
%Tensões de linha do gerador
%
VA=(V)*Va_Vb_Vc_pu(1,1)
VB=(V)*Va_Vb_Vc_pu(2,1)
VC=(V)*Va_Vb_Vc_pu(3,1)
V_aterramento=Zn*Ia0*(V/(sqrt(3)))
%
%Montagem dos vetores de corrente
%
origem =[0 0]; % Origem do sistema de vetores
pIA = [real(IA) imag(IA)]; % Vetor Corrente IA
dp1 = pIA-origem; % Diferença
pIB = [real(IB) imag(IB)]; % Vetor Corrente IB
dp2 = pIB-origem; % Diferença
pIC = [real(IC) imag(IC)]; % Vetor Corrente IC
dp3 = pIC-origem; % Diferença
%
pIa1 = [real(Ia1_A) imag(Ia1_A)]; % Vetor Corrente Ia1
dp4 = pIa1-origem; % Diferença
pIa2 = [real(Ia2_A) imag(Ia2_A)]; % Vetor Corrente Ia2
dp5 = pIa2-origem; % Diferença
pIa0 = [real(Ia0_A) imag(Ia0_A)]; % Vetor Corrente Ia0
origem_Ia0=dp4+dp5;
dp6 = pIa0-origem; % Diferença
%
pIb1 = [real(Ib1_B) imag(Ib1_B)]; % Vetor Corrente Ib1
dp7 = pIb1-origem; % Diferença
pIb2 = [real(Ib2_B) imag(Ib2_B)]; % Vetor Corrente Ib2
dp8 = pIb2-origem; % Diferença
pIb0 = [real(Ib0_B) imag(Ib0_B)]; % Vetor Corrente Ib0
origem_Ib0=dp7+dp8;
dp9 = pIb0-origem; % Diferença
%
pIc1 = [real(Ic1_C) imag(Ic1_C)]; % Vetor Corrente Ic1
dp10 = pIc1-origem; % Diferença
pIc2 = [real(Ic2_C) imag(Ic2_C)]; % Vetor Corrente Ic2
dp11 = pIc2-origem; % Diferença
pIc0 = [real(Ic0_C) imag(Ic0_C)]; % Vetor Corrente Ic0
origem_Ic0=dp10+dp11;
dp12 = pIc0-origem; % Diferença
%
title('Fasores de corrente e suas sequências');
figure(1)
hold on
grid on
grid minor
quiver(origem(1),origem(2),dp1(1),dp1(2),0) %IA
quiver(origem(1),origem(2),dp2(1),dp2(2),0) %IB
quiver(origem(1),origem(2),dp3(1),dp3(2),0) %IC
quiver(origem(1),origem(2),dp4(1),dp4(2),0) %Ia1
quiver(dp4(1),dp4(2),dp5(1),dp5(2),0) %Ia2
quiver(origem_Ia0(1),origem_Ia0(2),dp6(1),dp6(2),0) %Ia0
quiver(origem(1),origem(2),dp7(1),dp7(2),0) %Ib1
quiver(dp7(1),dp7(2),dp8(1),dp8(2),0) %Ib2
quiver(origem_Ib0(1),origem_Ib0(2),dp9(1),dp9(2),0) %Ib0
quiver(origem(1),origem(2),dp10(1),dp10(2),0) %Ic1
quiver(pIc1(1),pIc1(2),dp11(1),dp11(2),0) %Ic2
quiver(origem_Ic0(1),origem_Ic0(2),dp12(1),dp12(2),0) %Ic0
%hold off
legend('IA','IB','IC','Ia1','Ia2','Ia0','Ib1','Ib2','Ib0','Ic1','Ic2','Ic0')
legend show
%
%Montagem dos vetores de tensão de fase
%%
Vb0=Va0;
Vb1=Va1*((-1/2)-j*(sqrt(3)/2));
Vb2=Va2*((-1/2)+j*(sqrt(3)/2));
Vc0=Va0;
Vc1=Va1*((-1/2)+j*(sqrt(3)/2));
Vc2=Va2*((-1/2)-j*(sqrt(3)/2));
%
Va0_A=(Va0*(V/sqrt(3)))
Va1_A=(Va1*(V/sqrt(3)))
Va2_A=(Va2*(V/sqrt(3)))
Vb0_B=(Vb0*(V/sqrt(3)))
Vb1_B=(Vb1*(V/sqrt(3)))
Vb2_B=(Vb2*(V/sqrt(3)))
Vc0_C=(Vc0*(V/sqrt(3)))
Vc1_C=(Vc1*(V/sqrt(3)))
Vc2_C=(Vc2*(V/sqrt(3)))
%
pVA = [real(VA_fase) imag(VA_fase)]; % Vetor tensão VA_fase
dp13 = pVA-origem; % Diferença
pVB = [real(VB_fase) imag(VB_fase)]; % Vetor tensão VB_fase
dp14 = pVB-origem; % Diferença
pVC = [real(VC_fase) imag(VC_fase)]; % Vetor tensão VC_fase
dp15 = pVC-origem; % Diferença
%
pVa1 = [real(Va1_A) imag(Va1_A)]; % Vetor tensão Va1
dp16 = pVa1-origem; % Diferença
pVa2 = [real(Va2_A) imag(Va2_A)]; % Vetor tensão Va2
dp17 = pVa2-origem; % Diferença
pVa0 = [real(Va0_A) imag(Va0_A)]; % Vetor tensão Va0
origem_Va0=dp16+dp17;
dp18 = pVa0-origem; % Diferença
%
pVb1 = [real(Vb1_B) imag(Vb1_B)]; % Vetor tensão Vb1
dp19 = pVb1-origem; % Diferença
pVb2 = [real(Vb2_B) imag(Vb2_B)]; % Vetor tensão Vb2
dp20 = pVb2-origem; % Diferença
pVb0 = [real(Vb0_B) imag(Vb0_B)]; % Vetor tensão Vb0
origem_Vb0=dp19+dp20;
dp21 = pVb0-origem; % Diferença
%
pVc1 = [real(Vc1_C) imag(Vc1_C)]; % Vetor tensão Vc1
dp22 = pVc1-origem; % Diferença
pVc2 = [real(Vc2_C) imag(Vc2_C)]; % Vetor tensão Vc2
dp23 = pVc2-origem; % Diferença
pVc0 = [real(Vc0_C) imag(Vc0_C)]; % Vetor tensão Vc0
origem_Vc0=dp22+dp23;
dp24 = pVc0-origem; % Diferença
%
figure(2)
hold on
title('Fasores de tensão de fase e suas sequências')
grid on
grid minor
quiver(origem(1),origem(2),dp13(1),dp13(2),0) %VA
quiver(origem(1),origem(2),dp14(1),dp14(2),0) %VB
quiver(origem(1),origem(2),dp15(1),dp15(2),0) %VC
quiver(origem(1),origem(2),dp16(1),dp16(2),0) %Va1
quiver(dp16(1),dp16(2),dp17(1),dp17(2),0) %Va2
quiver(origem_Va0(1),origem_Va0(2),dp18(1),dp18(2),0) %Va0
quiver(origem(1),origem(2),dp19(1),dp19(2),0) %Vb1
quiver(dp19(1),dp19(2),dp20(1),dp20(2),0) %Vb2
quiver(origem_Vb0(1),origem_Vb0(2),dp21(1),dp21(2),0) %Vb0
quiver(origem(1),origem(2),dp22(1),dp22(2),0) %Vc1
quiver(dp22(1),dp22(2),dp23(1),dp23(2),0) %Vc2
quiver(origem_Vc0(1),origem_Vc0(2),dp24(1),dp24(2),0) %Vc0
hold off
legend('VA','VB','VC','Va1','Va2','Va0','Vb1','Vb2','Vb0','Vc1','Vc2','Vc0')
legend show

Accepted Answer

Kojiro Saito
Kojiro Saito on 16 Nov 2018
Edited: Kojiro Saito on 16 Nov 2018
In order to compile, you need to make your script to function and make the parameters (S,V,X0,X1,X2,Zaterramento_ohms) as input arguments.
function myTestFunc(S,V,X0,X1,X2,Zaterramento_ohms)
%
% Basta informar os 6 par?metros abaixo do gerador;
S = str2num(S);
V = str2num(V);
X0 = str2num(X0);
X1 = str2num(X1);
X2 = str2num(X2);
Zaterramento_ohms = str2num(Zaterramento_ohms);
% The rest is the same as your original scripts
% but I think you need to assign some value to variable "j"
"str2num" is added because the input parameters in Commmand Prompt would be characters.
Then from menu APPS, click "Application Compiler" app. From MAIN FILE, click plus button and add your m file as a main file, then click package.
After compile is succesful, go to for_redistribution_files_only folder, and launch command prompt, then type the following.
myFunc.exe 30000000 13800 0.08 0.2 0.25 571320
You can get figures as expected.
As you may know, in MATLAB, you can create GUI by AppDesigner (relatively new) or GUIDE (legacy). If you type appdesigner or guide in MATLAB command window, the GUI designer appears.
Or, MATLAB has an AppDesginer example, so if type
open Mortgage.mlapp
and the sample will appear.
You can compile it to a standalone application from "Share" -> "Standalone Desktop App" and the compiled exe file will work with MATLAB Runtime.
  3 Comments
Kojiro Saito
Kojiro Saito on 16 Nov 2018
It was a good news. Compiled application will be able to run without MATLAB, but with MATLAB Runtime.
Also, if you copy an installer exe file from for_redistibution folder to your target machine and launch the installer, MATALAB Runtime will be installed with your compiled exe.
Jucimar Carpe
Jucimar Carpe on 17 Nov 2018
Hi Kojiro,
I'm having two issues now that i'm starting on GUI world rsrs.
First: I would like to insert a gif image to be putted as a background on the right corner at the bottom. I already searched in many places how to do it but i didn't find a way to do it.
Second: How can I add a logo icon to my application? I've tried to do it after I packaged for the first time by windows but i was unable to do it.
Thanks in advance!!!

Sign in to comment.

More Answers (1)

Fred Smith
Fred Smith on 20 Nov 2018
Hi Jucimar,
MATLAB Compiler will let you package your application with a GUI as described by Kojiro above. However, if you need C or C++ code for the algorithm, you should investigate MATLAB Coder. MATLAB Coder will not help you with the GUI portion.
For a complete description of the differences between MATLAB Coder and MATLAB Compiler please refer to this answer here.
Good luck,
Fred

Categories

Find more on Get Started with MATLAB Compiler in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!