I am new to MATLAB. I am trying to create a simple program to find and display the risetime of pulse data in a .csv file. The program works for some files but not for other

조회 수: 2 (최근 30일)
I need to find the value and display a plot of the risetime for the data in a .csv file. The program works for some files but not for others. I get the error message shown below.
I get this error message when I run it:
Unable to perform assignment because the left and right sides have a different number of elements.
Error in Diff_Mode_Voltage_1kVPlot_BFP_NEW1_7_21 (line 256)
Full(i)=risetime(xr,fs,'PercentReferenceLevels',[10 90]);% %'s of levels
Below is the program. Plwase tell me why I get the error.
227 %RISE TIME---calculate the rise time of the waveform "(TestWaveform 1.csv')"
228 filename1='TestWaveform 1.csv";% reads the csv file as "file1"
229 opts = detectImportOptions(file1);
230 preview(file1,opts)
231 M1 = readmatrix(file1,'Range','D:E');% time is column D and the data value id in column E
232
233 [C,ia] = unique(M1(:,1));%unique fnction returns the data that is passed but removes all values that is repeated
234 M1 = M1(ia,:);
235
236 RT=risetime(M1(:,2),M1(:,1));% Names the rise time RT
237
238 for k = 1:2
239 RT= risetime(M1(:,2), M1(:,1));
240 [W,INITCROSS,FINALCROSS,MIDLEV] = pulsewidth(M1(:,2), M1(:,1));% do I need this???
241 PW{k,:} = {W,INITCROSS,FINALCROSS,MIDLEV};% do I need this???
242 end
243
244
245 %Measure/ calculate the rise time at the 10 to 90% points
246 Mylist1 = ls('CSV Files');%gives a list of all contents in the folder. This table can be displayed by entering "Rise" in the command window
247 newList1 = Mylist1(3:end,:);
248 Full = zeros(length(newList1(:,1)),1);%% do I need this???
249 for i = 1:length(newList1(:,1))
250 filename1 = convertCharsToStrings(newList1(i,:));
251 M1 = readmatrix(filename1,'Range','D:E');% M1 is used for rise time
252 tr=M1(:,1);% NOTE, tr is used in rise time
253 xr=M1(:,2);% NOTE, xr is used in rise time
254 fs = 1/(tr(2)-tr(1));
255 Full(i)=risetime(xr,fs,'PercentReferenceLevels',[10 90]);% %'s of levels
256 end
257
258 %%Calculate and plot the open circuit rise time
259 filename1 = convertCharsToStrings(newList1(9,:));%(9,:) tells the code which csv file in the "newList" folder to use
260 M1 = readmatrix(filename1,'Range','D:E');
261 tr=M1(:,1);
262 xr=M1(:,2);
263 figure% create a figure for the rist time plot
264 risetime(xr,tr);
265 axis([-0.3e-7, -.1e-7, 0, 7e4])% set the x min/max and y min/max in this order. Note: the Y azis will not plot below 0
266 saveas (gcf, 'plot_10.jpg') %save the plot to a file as "plot_10"
  댓글 수: 3
dpb
dpb 2021년 7월 23일
I've not used risetime extensively (it wasn't introduced while I was still in the active consulting gig or would have a lot), but I'd guess the problem is the failing files have more than one discernible bilevel signal transition based on the internal discretization done to set the high and low threshold levels.
If this were true, then there's only one index position availablel on the LHS which is a mismatch in length with the RHS.
Here's an example with a local array just happen to have and an arbitrary assignment of more than one value --
>> A(1)=[4 5];
Unable to perform assignment because the left and right sides have a different number of elements.
>>
Look familiar?? :)
dpb
dpb 2021년 7월 23일
Set a breakpoint @Line 255 and then execute the risetime call from command window and see what is returned.
To fix, either save the results as a cell array that can handle the variable number that can be returned or preprocess the signal to be sure there's only two discrete levels so only one transition will be located.

댓글을 달려면 로그인하십시오.

답변 (1개)

BP
BP 2021년 7월 24일
I set a breakpoint at line 255. It will run to that point with no issues. I get the error below after line 255. I have attached the code without line numbers below.
Errors generated:
Full(i)=risetime(xr,fs,'PercentReferenceLevels',[10 90]);% %'s of levels
Unable to perform assignment because the left and right sides have a different number of elements.
Error in Diff_Mode_Voltage_1kVPlot_BFP_NEW1_7_21 (line 256)
Full(i)=risetime(xr,fs,'PercentReferenceLevels',[10 90]);% %'s of levels
Code:
%RISE TIME---Calculate the rise time of the open circuit pulse_"V_Open_Circuit = readtable('2_70_O000_Ch4.csv')"
filename1='2_1k_O000_Ch4.csv';% reads the csv file as "filename1"
opts = detectImportOptions(filename1);
preview(filename1,opts)
M1 = readmatrix(filename1,'Range','D:E');
[C,ia] = unique(M1(:,1));%unique fnction returns the data that is passed but removes all values that is repeated
M1 = M1(ia,:);
RT=risetime(M1(:,2),M1(:,1));% Names the rise time RT
%[W,INITCROSS,FINALCROSS,MIDLEV] = pulsewidth(M(:,2),M(:,1));%NOTE W= width, INITCROSS= PULSE P= waveform at 50%,
for k = 1:2
RT= risetime(M1(:,2), M1(:,1));
[W,INITCROSS,FINALCROSS,MIDLEV] = pulsewidth(M1(:,2), M1(:,1));
PW{k,:} = {W,INITCROSS,FINALCROSS,MIDLEV};
end
%Measure/ calculate the oprn citcuit rise time 10 to 90% points
Mylist1 = ls('CSV Files');%gives a list of all contents in the folder. This table can be displayed by entering "Rise" in the command window
newList1 = Mylist1(3:end,:);
Full = zeros(length(newList1(:,1)),1);
for i = 1:length(newList1(:,1))
filename1 = convertCharsToStrings(newList1(i,:));
M1 = readmatrix(filename1,'Range','D:E');% M1 is used for rise time
tr=M1(:,1);% NOTE, tr is used in rise time
xr=M1(:,2);% NOTE, xr is used in rise time
fs = 1/(tr(2)-tr(1));
Full(i)=risetime(xr,fs,'PercentReferenceLevels',[10 90]);% %'s of levels
end
%%Calculate and plot the open circuit rise time
filename1 = convertCharsToStrings(newList1(9,:));%(9,:) tells the code which csv file in the "newList" folder to use
M1 = readmatrix(filename1,'Range','D:E');
tr=M1(:,1);
xr=M1(:,2);
figure% create a figure for the rist time plot
risetime(xr,tr);
axis([-0.3e-7, -.1e-7, 0, 7e4])% set the x min/max and y min/max in this order. Note: the Y azis will not plot below 0
saveas (gcf, 'plot_10.jpg')
  댓글 수: 2
dpb
dpb 2021년 7월 24일
편집: dpb 2021년 7월 24일
Of course you still get the same error, the point was to "Set a breakpoint @Line 255 and then execute the risetime call from command window and see what is returned."
When debugber halts, then what is the result in the command line of
risetime(xr,fs,'PercentReferenceLevels',[10 90])
???
That will show you what MATLAB is trying to store into the array location Full(i). It's going to be more than one value as the previous example code showed what generates the error message.
Show us the result of the above to prove it...
PS. Edit the code in the original Q? would be more better; use the code-edit buttons to do so...
dpb
dpb 2021년 7월 24일
How to fix depends upon what you want the results to be -- if only want/expect one value, then the problem lies in the input data and need to investigate why those files don't match the assumption of there being only one transition located.
Or, if there can be multiple (two or more) and those are ok, then you'll need to use a cell array instead for Full in order to be able to put more numbers than one into each cell. To do that "use the curlies, Luke" -- either on the LHS around the index or around the RHS expression to cast it to a cell. Then, the original allocation should be
Full = cell(numel(newList1),1);

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Modulation에 대해 자세히 알아보기

태그

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by