You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How can I resolve the subscripted assignment dimension mismatch error (for the cwt plot)?
1 view (last 30 days)
Show older comments
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.
Accepted Answer
Christopher McCausland
on 22 Nov 2023
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 Comments
Navid
on 22 Nov 2023
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
on 22 Nov 2023
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
on 22 Nov 2023
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
on 22 Nov 2023
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
on 22 Nov 2023
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
on 22 Nov 2023
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
on 22 Nov 2023
Your cfs is 3D because you have one layer for each sheet.
What would a 3D contour plot be like for you?
Navid
on 22 Nov 2023
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
on 22 Nov 2023
Edited: Christopher McCausland
on 22 Nov 2023
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
on 22 Nov 2023
Of course in the production code you can omit the
whos temp_cfs temp_f
which was added for debugging purposes,
Navid
on 23 Nov 2023
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
More Answers (0)
See Also
Categories
Find more on Data Import and Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)