이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
How can I resolve the subscripted assignment dimension mismatch error (for the cwt plot)?
조회 수: 1 (최근 30일)
이전 댓글 표시
Navid
2023년 11월 22일
Hello,
I have an issue with the syntax used for the wavelet plot. The syntax used is:
[cfs(:,i), f(:,i)] = cwt(signal(:,i),fs(:,i)).
I have placed this syntax inside a for loop, where 'i' is the Excel sheet name. However, I am facing an error saying, "Subscripted assignment dimension mismatch." Can you please guide me on how to fix this error? I would appreciate any help you can provide.
채택된 답변
Christopher McCausland
2023년 11월 22일
Hi Navid,
Can you please post so information in relation to both your signal and fs variable? Or better yet attach them?
cfs, should probably be a three dimetional array, so:
cfs(:,:,i)
as the cwt will return a two dimentional image if this code is sitting in a for loop. However, until you give us a bit more information this is hard to solve, for example your input signal could be in columns, or may not be.
댓글 수: 12
Navid
2023년 11월 22일
Dear Christopher McCausland,
I want to express my gratitude for your help. I have attached the Excel file containing the data, as requested. I appreciate your assistance in advance.
Thank you, and kind regards,
Navid
Walter Roberson
2023년 11월 22일
filename = "C1.xlsx";
S = sheetnames(filename);
for K = 1:numel(S)
s = S(K);
T = readtable(filename, 'sheet', s, 'VariableNamingRule','preserve');
[s, T.Properties.VariableNames]
end
ans = 1×3 string array
"CP1" "Time(s)" "Signal"
ans = 1×3 string array
"CP2" "Time(s)" "Signal"
ans = 1×3 string array
"CP3" "Time(s)" "Signal"
ans = 1×3 string array
"CP4" "Time(s)" "Signal"
ans = 1×3 string array
"CP5" "Time(s)" "Signal"
ans = 1×3 string array
"CP6" "Time(s)" "Signal"
ans = 1×3 string array
"CP7" "Time(s)" "Signal"
It is not clear to me how these inputs are converted into signal and fs ?
Christopher McCausland
2023년 11월 22일
Taking a bit of a guess here, we can work out the sampling frequency with f = 1/T. T appears to be 0.01 so f must be 100 Hz (do be careful of Nyquest etc.). And assuming that each sheet is a different signal. (If either if these statments are not true, then this code will need tweaked).
We can modifiy Walters code to add in the cwt command like so:
filename = "C1.xlsx";
S = sheetnames(filename);
for K = 1:numel(S)
s = S(K);
T = readtable(filename, 'sheet', s, 'VariableNamingRule','preserve');
[s, T.Properties.VariableNames]
[cfs(:,:,K), f(:,K)] = cwt(T.Signal(:,:),100);
end
% We can also have a look at the generated values
imagesc(abs(cfs(:,:,1)))
You should also note that fs is a fixed value, it doesn't appear to change in your code, I am not 100% sure how you were generating it, as incrementing though the time vector would give very wrong answers.
Hopefuly this helps!
Christopher
Navid
2023년 11월 22일
Dear Christopher McCausland,
Unfortunately, your code was accompanied by the following error:
"Undefined function or variable 'sheetnames'."
I have used the following code:
filename = 'C1.xlsx';
% Get the sheet names
[~, sheetNames] = xlsfinfo(filename);
for i = 1:numel(sheetNames)
% Read the data from the sheet
data = xlsread(filename, i);
% Extract the time and signal columns
t (:,i) = data(:, 1);
signal (:,i) = data(:, 2);
dt = t(2) - t(1); % sampling time
fs = 1/dt; % freq
end
Walter Roberson
2023년 11월 22일
Sorry, I did not notice the R2016b; sheetnames() was added in R2019b.
filename = 'C1.xlsx';
% Get the sheet names
[~, sheetNames] = xlsfinfo(filename);
for i = 1:numel(sheetNames)
% Read the data from the sheet
data = xlsread(filename, i);
% Extract the time and signal columns
t(:,i) = data(:, 1);
signal(:,i) = data(:, 2);
dt = mean(diff(t(:,i))); % sampling time
fs(i) = 1/dt; % freq
[temp_cfs, temp_f] = cwt(signal(:,i), fs(i));
whos temp_cfs temp_f
cfs(:,:,i) = temp_cfs;
f(:,i) = temp_f;
end
Name Size Bytes Class Attributes
temp_cfs 104x10015 16664960 double complex
temp_f 104x1 832 double
Name Size Bytes Class Attributes
temp_cfs 104x10015 16664960 double complex
temp_f 104x1 832 double
Name Size Bytes Class Attributes
temp_cfs 104x10015 16664960 double complex
temp_f 104x1 832 double
Name Size Bytes Class Attributes
temp_cfs 104x10015 16664960 double complex
temp_f 104x1 832 double
Name Size Bytes Class Attributes
temp_cfs 104x10015 16664960 double complex
temp_f 104x1 832 double
Name Size Bytes Class Attributes
temp_cfs 104x10015 16664960 double complex
temp_f 104x1 832 double
Name Size Bytes Class Attributes
temp_cfs 104x10015 16664960 double complex
temp_f 104x1 832 double
Navid
2023년 11월 22일
Dear Walter Roberson,
Thank you very much.
Coefficients were successfully obtained.
It is better to mention that in the next step, I have the following code:
figure; contour(t,f,abs(cfs).^2)
But The following error is displayed:
"Error using contour
Input arguments for contourc must have at most 2 dimensions."
Maybe it would have been better to first mention the contour plot issue in my question.
Thank you anyway.
Walter Roberson
2023년 11월 22일
Your cfs is 3D because you have one layer for each sheet.
What would a 3D contour plot be like for you?
Navid
2023년 11월 22일
Thank you for your time and consideration.
I am trying to generate the wavelet contour for each sheet in an Excel file. The code provided in the MATLAB documentation efficiently plots this contour for data in a single sheet. Please consider the following example used in the MATLAB documentation:
Fs = 1000;
t = 0:1/Fs:1-1/Fs;
x = 1.5*cos(2*pi*100*t).*(t<0.25)+1.5*cos(2*pi*50*t).*(t>0.5 & t<=0.75);
x = x+0.05*randn(size(t));
[cfs,f] = cwt(x,Fs);
contour(t,f,abs(cfs).^2);
axis tight;
grid on;
xlabel('Time');
ylabel('Approximate Frequency (Hz)');
title('CWT with Time vs Frequency');
Utilizing a loop would enable automatic plotting for multiple sheets. However, I cannot generalize the code for my specific requirement if implementing a for loop.
Christopher McCausland
2023년 11월 22일
편집: Christopher McCausland
2023년 11월 22일
Hi Navid,
Can I ask why you want to do this?
While we can include plots in for loops, its generally considered bad practice. I am also not convinced about using contour as I do not think it convays the information well.
Regardless, please see attached contour as requested, and in addition via mesh() which I personally think convays the information better. The y-axis has been set to log to reflect the nature of the CWT. You should also note that this information is the same as a 2D cwt plot with a colour scale.
Christopher
filename = 'C1.xlsx';
% Get the sheet names
[~, sheetNames] = xlsfinfo(filename);
for i = 1:numel(sheetNames)
% Read the data from the sheet
data = xlsread(filename, i);
% Extract the time and signal columns
t(:,i) = data(:, 1);
signal(:,i) = data(:, 2);
dt = mean(diff(t(:,i))); % sampling time
fs(i) = 1/dt; % freq
[temp_cfs, temp_f] = cwt(signal(:,i), fs(i));
whos temp_cfs temp_f
cfs(:,:,i) = temp_cfs;
f(:,i) = temp_f;
% Begin plotting here, typically we would not do so in a for loop.
figure()
contour(t(:,i),f(:,i),abs(cfs(:,:,i)).^2);
set(gca, 'YScale', 'log');
% Here is the same information presented with Mesh, which convays the data trends
% better in my opinion.
figure()
mesh(t(:,i),f(:,i),abs(cfs(:,:,i)).^2)
view(-30,60)
set(gca, 'YScale', 'log');
end
Walter Roberson
2023년 11월 22일
Of course in the production code you can omit the
whos temp_cfs temp_f
which was added for debugging purposes,
Navid
2023년 11월 23일
Dear Christopher,
I wanted to express my gratitude for the valuable guidance you provided me. This part of the code:
mesh(t(:,i),f(:,i),abs(cfs(:,:,i)).^2)
truthfully gives a deeper understanding of the signal.
However, sometimes, a 2D cwt plot may be more suitable for monitoring specific frequency ranges of a signal.
Thank you for taking the time to share your knowledge with me. Your guidance has been instrumental in helping me improve my work, and I greatly appreciate it.
Best regards,
Navid
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Import and Analysis에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
아시아 태평양
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)