Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
8 years, 1 month ago.
Plotting from Mbed to MATLAB
Hi all, Hope someone can help me with this:)
I currently have this code with me , it stores 1000 data from my accelerator sensor and saves it into a Excel file. I would now like to make MATLAB plot the graph for me and also point out the peaks of the waveform
Thank you in advance :)
<<code>>
- include "mbed.h" LocalFileSystem local("local"); Serial pc(USBTX,USBRX); Ticker in; AnalogIn x_in(p17); AnalogIn y_in(p18); AnalogIn z_in(p19);
const int maxReadings = 1000; float measurmentArray[maxReadings][3]; volatile int readingNumber = 0;
void update() { if (readingNumber < maxReadings) { measurmentArray[readingNumber][0]=x_in; measurmentArray[readingNumber][1]=y_in; measurmentArray[readingNumber][2]=z_in; readingNumber++;}}
main () { int nextReading = 0; in.attach_us(&update,10000);1000 hertz
while (nextReading<maxReadings ) { process until no more space if (readingNumber > nextReading) { there is new data nextReading = readingNumber-1; the index of the last value written to the array float x,y,z; copy values to a local variables x=measurmentArray[nextReading][0]; y=measurmentArray[nextReading][1]; z=measurmentArray[nextReading][2];
pc.printf("X-%5.2f Y-%5.2f Z-%5.2f \n",x, y, z); nextReading++; } } write array in the file txt/csv FILE *fp = fopen("/local/Record.csv", "a"); if (fp) { for (int i=0;i<maxReadings;i++) {
fprintf(fp, "%f , %f , %f \r ",measurmentArray[i][0],measurmentArray[i][1],measurmentArray[i][2]);
} fclose(fp); } else { pc.printf("ERROR. Failed to open output file\r\n"); } }
<</code>>