How I can Plot Data Samples as Data values in Y-axis and Sample counter converted to micro second values in X-axis? Then get average and standard variation values?

I imported the excel file which have Two coloumns and 100,001 rows.
1- I use the plot icon in MATLAB to plot all of the rows in the first column. for example below:
2- Then, I get the below graph:
2- How I can do this plot again, but insetad of the Sample counter values in x-axis, I want to make it (time).
For example, convert the first sample = 1 TO BE first sample = 1e-06 (micro seconds) and to all of the other samples till I reach 100,001 e-06. I can explain it again below:
  1. The distance between samples is 1us (microsecond).
  2. We take the samples, create time axis. And, We’ve got 100,001 point.
  3. So we got time samples, or sample counter (SC), and we translate this into time, Which will be 100,001 * 1 us (micro seconds) which will be 100,000 us (microseconds)
3- After I finish from above steps, I will have my samples in the y-axis and I will have the time in micoseconds in x-axis.
4- Then I want to plot The pulse,which is in 50 us, that’s the ON time (included in the plot). And, the OFF time is 950 us (not in plot). However:
  1. The start of analysis of 1st window, is 15us + 5 um. That’s our start.
  2. The end of first-time window is 15us + 5us + 40us. Thats our end. Then we can plot it.
5- So after we finish from the first pulse above which is considers as number 0. Then we have to do it to the rest for pulses which will be for example from 1 to 99. And we use a counter, for example,"m" to be used for 1 to 99 in below equation:
  1. 15us + 5us + 40us + m*1000us.
  2. Therefore I need to repeat for example, using "for" loop.
  3. and then we just hold on for the 40us AND hold off for the rest in each loop. Then plot all of those windows and then we should have a line.
6- Also when we do the “for” loop, for each time window , I want to calculate average as well AND standard deviation. I also want to plot thses and see if there a trend

Réponses (1)

I highly recommend going through MATLAB Onramp, but the quick answers are
2. Select your x values, then use Ctrl+click to select your y values (should have 2 columns selected. Not go to the plots tab and select the plot you want. See this video. this approach will not work if you plan to loop your code, however. You must write the actual code.
4. The easiest way to plot is to create a vector of your pulse values, and a corresponding vector of time values. Then plot them using the appropriate plotting function. To add 2 lines to the same plot, use hold on followed by hold off when done.
6. Taking the average and standard devaition are just a matter of using the mean and std functions with the appropriate data. MATLAB Onramp will introduce you to using MATLAB functions as well.

15 commentaires

Thanks Cris. I wathced the video and I went over Onramp.
First of all, translate the samples counter into time? can you guide me in this?
When I try to plot using the sytax below in MATLAB:
plot(s11_mag)
I get the below graph:
I need to convert the x-axism which is the sample coutner to time of 1 micro second for each 1 counter:
1 counter = 1 micro second
How I can do it?
If time is not captured in your data set, you can create it easily enough. This is covered in Ch 4 of MATLAB Onramp (Vectors and Matrices > Creating Evenly-Spaced Vectors). Make sure you complete all 7 tasks plus the Further Practice.
I went throght it. thanks. but still unable to get it done.. i need to convert the sample counter with different values wihtout changing the distance in the x-axis
% Automated code for data imported from excel
M = MATLABDATAExp7
%==
%This is for A2 =A
A2 = M(:,1)
%numericScores = scores(:,3:end)
numericVars = vartype("I_inc")
numericScores = A2(:,numericVars)
numericScores = A2
A = numericScores{:,:}
%==
%This is for C2 =B
C2 = M(:,3)
numericVars = vartype("Q_inc")
numericScores = C2(:,numericVars)
numericScores = C2
B = numericScores{:,:}
%==
complex(A,B)
E2 = complex(A,B)
%This is for B2 =C
B2 = M(:,2);
% don’t put this again numericScores = scores(:,3:end);
numericVars = vartype("I_ref");
numericScores = B2(:,numericVars);
numericScores = B2;
C = numericScores{:,:}
%==
%This is for C2 =D
C2 = M(:,4)
numericVars = vartype("Q_ref")
numericScores = C2(:,numericVars)
numericScores = C2
D = numericScores{:,:}
complex(C,D)
F2 = complex(C,D)
%=========
% getting IMABS
Inc_mag = abs(E2);
Ref_mag = abs(F2);
% done with IMABS
% ===========
% Inc_phase [deg]  IMARGUMENT(E2)*180/PI()
angle(E2) * 180/pi
Inc_phase_deg = angle(E2) * 180/pi
% Ref_phase [deg]  =IMARGUMENT(F2)*180/PI()
angle(F2) * 180/pi
Ref_phase_deg = angle(F2) * 180/pi
%==
% s11  =IMDIV(F2,E2) means division of the complex number , which is = F2 / E2
%s11 = (F2 / E2), we use Right array division, for example, x= A./B
s11 = F2./E2
%s11_mag  IMABS(K2) --> abs(s11);
s11_mag = abs(s11)
%==
%s11_phase_deg  IMARGUMENT(K2)*180/PI() -----> IMARGUMENT(s11)*180/PI()
s11_phase_deg = angle(s11) * 180/pi
%==
plot(s11_mag)
i ATTACHED the excel file which contain the data.
I also shared the code so you can look at the result which is the plot for s11_mag.
Im trying to change only the values of the sample counters without changing anythign in the graph so that I can later make a for loop to plot the siganls in a specific time period which repeat in every 1000, as follow:
15us + 5us + 40us + m*1000us.
"m" from 1 to 99
Then we just hold on for the 40us AND hold off for the rest in each loop. Therefroe we can plot all of those ON windows and then we should have a line.
I'm not sure how you are reading your data in, but I would suggest using readtable. This might make accessing your data easier. See this page.
It doesn't yet look like you have tried to create a time vector. You provide the equation in your question, so why not just implement that?
  • 100,001 * 1 us (micro seconds)
Here is how I might approach this.
file = "https://www.mathworks.com/matlabcentral/answers/uploaded_files/1140105/MATLAB%20DATA%20-%20Exp%207.xlsx";
M = readtable(file)
M = 100000×4 table
I_inc I_ref Q_inc Q_ref ___________ ___________ ___________ ___________ 0.0007087 0.00077618 0.00047246 0.00025873 -0.00047246 -0.00038809 -0.00094493 -0.00012936 0 0.00038809 0.00047246 0.00012936 0.00035435 0.00025873 0.00023623 -0.00012936 -0.0012993 -0.0011643 -0.0011812 0.00012936 0.003071 0.0031047 0.0035435 0 -0.031655 -0.029107 -0.036616 0 -0.061302 -0.051874 -0.060475 0.0046571 -0.058822 -0.045148 -0.047364 0.0086673 -0.064964 -0.047735 -0.048309 0.011255 -0.065554 -0.045536 -0.042758 0.013712 -0.068035 -0.045536 -0.040514 0.015653 -0.070161 -0.044501 -0.037443 0.017852 -0.071224 -0.043466 -0.033663 0.019922 -0.073114 -0.042819 -0.031183 0.021862 -0.074059 -0.041267 -0.026812 0.024061
A = M.I_inc;
B = M.Q_inc;
E2 = complex(A,B);
C = M.I_ref;
D = M.Q_ref;
F2 = complex(C,D);
% getting IMABS
Inc_mag = abs(E2);
Ref_mag = abs(F2);
Inc_phase_deg = angle(E2) * 180/pi;
Ref_phase_deg = angle(F2) * 180/pi;
%==
s11 = F2./E2;
s11_mag = abs(s11);
s11_phase_deg = angle(s11) * 180/pi;
%==
t = (1:length(s11_mag))*1e-6;
plot(t,s11_mag)
ylabel('Magnitude')
xlabel('Time (s)')
THANK YOU SO MUCH FOR YOUR SUPPORT AND GUIDANCE. IT MADE IT MORE CLEAR NOW AFTER CONNECTING ALL THE INFORMATION YOU GIVEN AND LOOKING AT ALL OF THE NOTES AND SUBJECTS YOU RECOMMENDED AND THIS CODE EXPLANATION MADE IT SOUND AND CLEAR
APPRECIATING YOUR SUPPORT AND EFFORTS
Now I need to:
Extract specific values in specific time and then plot them all.
  1. The start of analysis of 1st window, is 15us + 5 um. That’s our start.
  2. The end of first-time window is 15us + 5us + 40us. Thats our end.
  3. THEN we repeat it for each 1000 micro second (us). THEREFORE, we should use for loop. The variable is m=1:99 STATATEMET is t=15us + 5us + 40us + m*1000us
  4. The pulse,which is in 40 us, that’s the ON time (included in the plot). And, the OFF time is 950 us (not in plot)
  5. Plot the new graph?
What have you tried? Share your code.
Ch 5 of MATLAB Onramp covers indexing arrays, if that is what you mean.
m=0:1:99
for
t= 15.*1e-6 + 5.*1e-6 + 40.*1e-) + m.*1000.*1e-6;
end
t;
i can use excel to do it. but i choose to learn and use MATLAB
Ch 12 of MATLAB Onramp covers for loops. Here's a simple example
for v = 1.0:-0.2:0.0
disp(v)
end
1 0.8000 0.6000 0.4000 0.2000 0
I don't quite follow what it is you are describing. What is being plotted?
This is the plot result from my code:
Now, I will zoom in the plot and you can see below
From above, you can notice I have pulses which repeat ever 1000 micro second. Those pulses are my target.
For example: Looking at the zoom in for the first data pulse below:
You can look from above zoom in. The first pusle start from 7 micro seconds (us) which is to be more precise maybe from almost 5 us and it end at 57 us which is almost 60 us.
Looking again for the next pulse in graph below:
In above graph, we can notice a pattern for the second pulse, Its start from 15us and it end at 60us.
This means in every 1000us, we have 950us noise and only 40us is the data captured which is pulse.
Therefore from this 100,000 us. I have only 100 pulse which is repeated every 1000us.
So, each pulse have a time window which start at 15 u + 5us AND it ends at 15us+5+40us which is 60us.
I want to make a "for" loop which will let me extract the pulse which occure from 0 till 100,000 us.
Then we will get 100 pulses. In each pulse the magnitude value is changin in the 40us.
I need to take this avreage value in each pulse and then plot those values to visulaize it if its increasing or decreasing.
For example you can look at the excel sheet screen shoot below:
I was doing this manually for the frist, second and third pulse for all my data, which is time consuming and not accurate. From above excel sheet, you can see I chose the values of magnitude in the first puls and wrote its avreage values then did the same for the second and third pulse. After that I took the avreage of the three pulses to have a final value for the first data then i did this again for the next data..etc
I need to learn this so that i can automate it in MATLAB. I was using MATLAB and EXCEL. Now, i just want to use MATLAB which I concluded it will make my result more visual and more faster in calculation.
Here, I think it is easier to work in indices rather than time. Again, you have the equation for the indices you want already, so now just incorporate it into your code. I'm not sure what exactly you want your final plot to be, so I took a guess by plotting the mean of mag and phase over each 40 pulse.
% Start is the same as before
file = "https://www.mathworks.com/matlabcentral/answers/uploaded_files/1140105/MATLAB%20DATA%20-%20Exp%207.xlsx";
M = readtable(file)
M = 100000×4 table
I_inc I_ref Q_inc Q_ref ___________ ___________ ___________ ___________ 0.0007087 0.00077618 0.00047246 0.00025873 -0.00047246 -0.00038809 -0.00094493 -0.00012936 0 0.00038809 0.00047246 0.00012936 0.00035435 0.00025873 0.00023623 -0.00012936 -0.0012993 -0.0011643 -0.0011812 0.00012936 0.003071 0.0031047 0.0035435 0 -0.031655 -0.029107 -0.036616 0 -0.061302 -0.051874 -0.060475 0.0046571 -0.058822 -0.045148 -0.047364 0.0086673 -0.064964 -0.047735 -0.048309 0.011255 -0.065554 -0.045536 -0.042758 0.013712 -0.068035 -0.045536 -0.040514 0.015653 -0.070161 -0.044501 -0.037443 0.017852 -0.071224 -0.043466 -0.033663 0.019922 -0.073114 -0.042819 -0.031183 0.021862 -0.074059 -0.041267 -0.026812 0.024061
A = M.I_inc;
B = M.Q_inc;
E2 = complex(A,B);
C = M.I_ref;
D = M.Q_ref;
F2 = complex(C,D);
% getting IMABS
Inc_mag = abs(E2);
Ref_mag = abs(F2);
Inc_phase_deg = angle(E2) * 180/pi;
Ref_phase_deg = angle(F2) * 180/pi;
%==
s11 = F2./E2;
s11_mag = abs(s11);
s11_phase_deg = angle(s11) * 180/pi;
%==
t = (1:length(s11_mag))*1e-6;
plot(t,s11_mag)
ylabel('Magnitude')
xlabel('Time (s)')
% define pulse characteristics
start = 20;
width = 40;
gap = 1000;
% capture results in a new table, pStats
pStats = table('Size',[100 3],'VariableTypes',{'double','double','double'},'VariableNames',["Time","Avg_mag","Avg_phase"]);
ind = start:gap:length(s11_mag);
for p = 1:length(ind)
magdata = s11_mag(ind(p):(ind(p)+width));
phasedata = s11_phase_deg(ind(p):(ind(p)+width));
pStats.Time(p) = t(ind(p));
pStats.Avg_mag(p) = mean(magdata);
pStats.Avg_phase(p) = mean(phasedata);
end
% view results
pStats
pStats = 100×3 table
Time Avg_mag Avg_phase _______ _______ _________ 2e-05 0.6069 -49.839 0.00102 0.6084 -49.597 0.00202 0.60818 -50.096 0.00302 0.60956 -49.635 0.00402 0.60508 -49.805 0.00502 0.60838 -49.696 0.00602 0.60864 -49.7 0.00702 0.61456 -49.906 0.00802 0.60383 -49.556 0.00902 0.60549 -49.495 0.01002 0.60312 -49.277 0.01102 0.60495 -50.126 0.01202 0.60417 -49.947 0.01302 0.60335 -49.592 0.01402 0.6058 -49.545 0.01502 0.60667 -49.533
figure
yyaxis left
plot(pStats,"Time","Avg_mag")
ylabel('Magnitude')
xlabel('Time (s)')
yyaxis right
plot(pStats,"Time","Avg_phase")
ylabel('Phase (deg)')

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Produits

Version

R2022b

Question posée :

Ali
le 27 Sep 2022

Modifié(e) :

le 30 Sep 2022

Community Treasure Hunt

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

Start Hunting!

Translated by