how can i convert the following script into function??

조회 수: 2 (최근 30일)
Sanjeev Ramachandran
Sanjeev Ramachandran 2018년 12월 12일
편집: Walter Roberson 2018년 12월 12일
I am having trouble in converting the following Script into function
fid = fopen('C:\Users\Windows\Downloads\INPUT_1.txt', 'r+');
data1 = zeros(4007,1);
data1 = fscanf(fid, '%f');
fclose(fid);
plot(data1);
b = [0.0305, 0.0305];
a = [1, -0.9391];
zi = 0.9695;
y = filter(b, a, data1, data1(1)*zi);
fid = fopen('C:\Users\Windows\Downloads\RESULT.txt', 'w+');
[m1, n1] = size(data1)
for i = 1:m1
fprintf(fid, '%f\r\n',data1(i,1));
end
fclose(fid);

답변 (2개)

madhan ravi
madhan ravi 2018년 12월 12일
편집: madhan ravi 2018년 12월 12일
save it as main1.m file it becomes a function now you can also Parameterize your function if you want to...
function main1
your script
end
  댓글 수: 4
Sanjeev Ramachandran
Sanjeev Ramachandran 2018년 12월 12일
Yes I saved it as main1.m
madhan ravi
madhan ravi 2018년 12월 12일
Paste the complete error message and the function you are trying with a sample file to test also how do you call the function?

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2018년 12월 12일
function main1(filename_in, filename_out)
if nargin < 2
error('Not enough input arguments')
end
[fid, msg] = fopen(filename_in, 'r+');
if fid < 0
error('Failed to open input file "%s" because "%s"', filename_in, msg);
end
data1 = zeros(4007,1);
data1 = fscanf(fid, '%f');
fclose(fid);
plot(data1);
b = [0.0305, 0.0305];
a = [1, -0.9391];
zi = 0.9695;
y = filter(b, a, data1, data1(1)*zi);
[fid, msg] = fopen(filename_out, 'w+');
if fid < 0
error('Failed to open output file "%s" because "%s"', filename_out, msg)
end
[m1, n1] = size(data1)
for i = 1:m1
fprintf(fid, '%f\r\n',data1(i,1));
end
fclose(fid);
end
  댓글 수: 2
Sanjeev Ramachandran
Sanjeev Ramachandran 2018년 12월 12일
I tried it but I am getting as build failed
Walter Roberson
Walter Roberson 2018년 12월 12일
편집: Walter Roberson 2018년 12월 12일
Please show in more detail what you did ?
Your use of the word "build" suggests to me you might be using MATLAB Compiler, and that your reason for converting to a function might be to be able to create an executable from the code.
If that is your purpose, and you always want to use the same file names, then follow madhan's procedure of just putting a "function" statement at the top of the code with the function name matching the name of the file (which would have to be a MATLAB identifier.)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

제품


릴리스

R2013a

Community Treasure Hunt

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

Start Hunting!

Translated by