現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
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
Navid
2023 年 11 月 23 日
Dear Walter Roberson, Your support is greatly appreciated. Thank you so much!
その他の回答 (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!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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 (한국어)