How can I convert codes from MATLAB to Arduino
15 views (last 30 days)
Show older comments
Hello, I'm newly in MATLAB and Arduino. I have known that MATLAB could convert codes from m files to C code. So I think it possible to get C code and then compile to Arduino hardware. Please give me some direction, How can I do that?
Thanks, chirawat
1 Comment
dawit abadi
on 9 Jun 2018
i also have arduino code and i want to convert it to matlab code ,how can i do it
Answers (2)
Jan
on 15 Nov 2012
This is not trivial and it depends on the code itself. E.g. calls of input or ode45 will not run on the Arduino. Could you post the code, which you want to convert?
4 Comments
Mirza Riyasat Ali
on 30 Jul 2020
Edited: Walter Roberson
on 6 Aug 2020
can you help me converting this code to arduino
% Matlab Program to demonstrate the concpet of "Signal Smoothing"
% Signal Smoothing or Averaging is the Fundamental Noise Reduction Tool in
% 1-D Signal Processing Such as a Monotonic Signal, Speech or Voice.
clc;
clear all;
close all;
% Initialise the Time
t = 0:0.001:1; % 0 -- Starting Time, 0.001-- Sampling Time, 1 -- Ending Time
% reading the size of the time
[m,n] = size(t);
% Generation of sine wave
% amplitude
a = 2;
% frequency
f = 4;
% sine wave argument according to the definition
y = a*sin(2*pi*f*t);
% Generation of Random signal using "rand" command in the matlab
% Please see the sintax of "rand" command by typing help rand in the matlab
% command window
r = rand(m,n);
% Note: Observer that, we generated the random signal in the matlab using
% rand function such that, size of the signal and random signal both are
% similar.
% Adding both signal and noise
y1 = (y + r);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%Implementation of Moving Average Filter or Smoothing Filter%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Algorithm:
% X(n-1) + X(n) + X(n+1)
% Y(n) = ----------------------
% 3
%
% here, X(n) = noise effected signal
% Y(n) = Smoothed or averaged signal
% therefore,
% Let's Initiliaze the for loop from 2:n-1 in order to validate the real
% meaning of (n-1) and (n+1)
% let the ouput smoothed signal is y2(n)
% Let's create an empty array y2(n), size equal to the size of the input
% signal
y2 = zeros(m,n);
for i = 2:(n-1)
y2(i) = (y1(i-1) + y1(i) + y1(i+1))/3;
end
% therefore y2(i) will be the smoothed signal
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Lets plot the signals and see the output
subplot(411);
plot(t,y); title('input signal');
subplot(412);
plot(t,r); title('random signal');
subplot(413);
plot(t,y1); title('noise added signal');
subplot(414);
plot(t,y2); title('smoothed signal');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Please try this implementation an extension
% initiate a for loop for 2-4 times, and make y2 = y1, i.e., we are making
% a feed back, it becomes a recursive filter, this will be the first step
% towards the implementation of Recursive filter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% For any questions please send a mail to samudrala.naren@gmail.com
% Implemented by : Jagadeesh Samudrala, Asst. Prof., Dept. of ECE, Aditya
% Engineering College.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Walter Roberson
on 6 Aug 2020
Arduino does not support plotting, so that code cannot be converted.
The numeric part up to the calculation of y2 could be generated for Arduino, if you had the MATLAB Coder toolbox, or if you are willing to embed the code within a Simulink project (Simulink can generate code for Arduino even without MATLAB Coder, but you would have to wrap some of the functionality within a MATLAB Function Block.)
See Also
Categories
Find more on Arduino Hardware in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!