How can I use for loop for assigning variable names?

1 visualizzazione (ultimi 30 giorni)
tahsin
tahsin il 30 Apr 2015
Commentato: Stephen23 il 23 Apr 2023
How can I use for loop for assigning variable names and shorten this code thank you.
clc; clear all; close all;
format long g;
%Reads data from excel that transferred from Dewesoft program
data=xlsread('data/Test_2014_05_01_IMP_R3_Darbe 3m.xlsx');
dt=0.0002; %Sampling period 5000hz
time=data(:,1);
%accelerometer data obtained
acc1=data(:,2);
acc2=data(:,3);
acc3=data(:,4);
acc4=data(:,5);
acc5=data(:,6);
%loadcell data obtained
loadcell=data(:,7);
%potentiometer data obtained
pot1=data(:,8);
pot2=data(:,11);
pot3=data(:,12);
pot4=data(:,13);
pot5=data(:,14);
pot6=data(:,15);
pot7=data(:,16);
%Remove linear trends
acc01=detrend(acc1);
acc02=detrend(acc2);
acc03=detrend(acc3);
acc04=detrend(acc4);
acc05=detrend(acc5);
%Trapezoidal numerical integration without removing linear trends
vel1=cumtrapz(acc1)*dt;
vel2=cumtrapz(acc2)*dt;
vel3=cumtrapz(acc3)*dt;
vel4=cumtrapz(acc4)*dt;
vel5=cumtrapz(acc5)*dt;
%Trapezoidal numerical integration with removed linear trends
vel01=cumtrapz(acc01)*dt;
vel02=cumtrapz(acc02)*dt;
vel03=cumtrapz(acc03)*dt;
vel04=cumtrapz(acc04)*dt;
vel05=cumtrapz(acc05)*dt;
%-------------------------
disp1= cumtrapz(vel1)*dt;
disp2= cumtrapz(vel2)*dt;
disp3= cumtrapz(vel3)*dt;
disp4= cumtrapz(vel4)*dt;
disp5= cumtrapz(vel5)*dt;
%-------------------------
disp01= cumtrapz(vel01)*dt;
disp02= cumtrapz(vel02)*dt;
disp03= cumtrapz(vel03)*dt;
disp04= cumtrapz(vel04)*dt;
disp05= cumtrapz(vel05)*dt;
%---------------------------
accplcmax1 = find(acc1==max(acc1));
accplcmin1 = find(acc1==min(acc1));
accstrmax1=num2str(max(acc1));
accstrmin1=num2str(min(acc1));
accplcmax2 = find(acc2==max(acc2));
accplcmin2 = find(acc2==min(acc2));
accstrmax2=num2str(max(acc2));
accstrmin2=num2str(min(acc2));
accplcmax3 = find(acc3==max(acc3));
accplcmin3 = find(acc3==min(acc3));
accstrmax3=num2str(max(acc3));
accstrmin3=num2str(min(acc3));
accplcmax4 = find(acc4==max(acc4));
accplcmin4 = find(acc4==min(acc4));
accstrmax4=num2str(max(acc4));
accstrmin4=num2str(min(acc4));
accplcmax5 = find(acc5==max(acc5));
accplcmin5 = find(acc5==min(acc5));
accstrmax5=num2str(max(acc5));
accstrmin5=num2str(min(acc5));

Risposte (2)

pfb
pfb il 30 Apr 2015
Modificato: pfb il 30 Apr 2015
what's wrong with using matrices? Something like
acc = data(:,2:6);
pot = data(:,8:16);
acc0 = detrend(acc);
and so on?
Of course you need to use vectorized functions.
For instance, both detrend and cumtrapz are already vectorized (by default along columns).
vel = cumtrapz(acc)*dt;
vel0 = cumtrapz(acc0)*dt;
In this case each column of vel and vel0 holds your old vel1...vel5 and vel01...vel05, respectively.
Your code is significantly shortened, without the need of defining variable names, which is a bad practice, as explained in the answer referenced by the cyclist.

the cyclist
the cyclist il 30 Apr 2015
You probably want to use cell arrays.
This prior question/answer discusses the dynamic assignment of variable names.

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by