Hi, I have a code in MATLAB which tries to zero detect, i am trying to convert the same to C but i am having trouble loading the data.

1 回表示 (過去 30 日間)
While running the program in matlab coder, the value of y is taken to be 1, and it does not go to the for loop.I have attached the file(.mat) from which i am getting the signal. The code is:
%cd 'C:\Users\HP\Desktop\Project Simulation\water\WaterBonePlaneInterface\SimulationResults'
%plot frequency spectrum of pulse transmitted through water in time domain
function [g,h,j,o]=generatezeros
%cd C:\Users\HP\Desktop\Project Simulation\water\WaterBonePlaneInterface\SimulationResults\;
y=coder.load('C:\Users\HP\Desktop\Project Simulation\water\WaterBonePlaneInterface\SimulationResults\B.mat');
%y=[-1 1 1 1 -1 -1 -1 2 3 1 1 1 -1];
g=0;
h=0;
j=0;
o=0;
k=0;
coder.extrinsic('display');
for i = 1:length(y)-1
if ((sign(y(i+1)) == 1 && sign(y(i))==-1) || (sign(y(i+1))==-1 && sign(y(i))==1))
k=k+1;
display(k);
if (k==1)
g=i;
elseif (k==2)
h=i;
elseif (k==3)
j=i;
else (k==4)
o=i;
end
end
end
display(length(y));
display(g);
display(h);
display(j);
display(o);

採用された回答

Kunal Khosla
Kunal Khosla 2019 年 5 月 30 日
I did a abit of experiment and was able to figure out the issue. The lines should be changed to
s=coder.load('C:\Users\HP\Desktop\Project Simulation\water\WaterBonePlaneInterface\SimulationResults\B.mat');
y=s.y;
Then the matlab data is loaded on to the C program.

その他の回答 (2 件)

Guillaume
Guillaume 2019 年 5 月 30 日
The name of your function generatezeros is very misleading since what that function does is find the first 3 and the last indices where a vector changes sign. It does not generate anything in any way. Considering that there is no comment in your code, at least use a function name that explains what it actually does.
Note that
else (k==4)
is the same as
else
k == 4 %pointless line that will either print 1 or 0 on the command line
it is not equivalent to
elseif k==4
This else wil be executed for all k greater than 3, each time setting o to the index i. So at the end of the loop o will be the index of the last sign change in the vector. If you meant o to be the 4th one, then that's not what your code does.
In any case, the whole function can be simplify to:
function locations = findzerocross(y)
%input: y a vector of numbers
%output: the location of all zero-crossing points
locations = find(diff(sign(y)) ~= 0);
end
If you want the first four locations, you can then index the returned array
y = coder.load('C:\Users\HP\Desktop\Project Simulation\water\WaterBonePlaneInterface\SimulationResults\B.mat');
zerocrossing = findzerocross(y);
zerocross4 = zerocrossing(1:4);
I know nothing about coder, so no idea what you need to do to make it compile.

Kunal Khosla
Kunal Khosla 2019 年 5 月 30 日
Thanks a lot. Any one with prior Matlab coder experience.

カテゴリ

Help Center および File ExchangeMATLAB Coder についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by