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 (한국어)