How do I use the cat function?

10 ビュー (過去 30 日間)
Mackenzie Maher
Mackenzie Maher 2021 年 12 月 13 日
コメント済み: Mackenzie Maher 2021 年 12 月 17 日
HI everyone!
I created the following code to obtain certain data points (peaks). I'm trying to use the the cat function to get all of the peaks into one array (Allpeaks). However when i did this I got the erorr below:
fieldNames = fieldnames(MCR_full.MIB037.Reaches);
for k = 1 : 10
thisFieldName = fieldNames{k};
thisArray = MCR_full.MIB037.Reaches.(thisFieldName).kin;
x = thisArray(:, 1);
y = thisArray(:, 3);
Allpeaks=cat(peaks)
[peaks, locs]= findpeaks (y);
reachLimitArray= find (peaks >= 0.12);
if length(reachLimitArray )> 1
disp('There is at least two value above the limit.');
for i = 1 : length(reachLimitArray)
end
else
disp('All values are below the limit.');
end
end
Error using cat
Dimension argument must be a real, positive, integer scalar.
If anyone knows what this means and how to fix it your help would be greatly appreciated!
  2 件のコメント
Mackenzie Maher
Mackenzie Maher 2021 年 12 月 13 日
Thanks for both of those tips and what you suggested worked. However, I think concatenate is the wrong function. I’m looking for one that stores all the data I need and prevents the loop from overwriting every time it cycles through. Any ideas on how to do this?
Stephen23
Stephen23 2021 年 12 月 13 日
"Any ideas on how to do this?"
The standard approach is to use indexing into an array:

サインインしてコメントする。

採用された回答

Jan
Jan 2021 年 12 月 13 日
Whenever you have an error for a specific command, read the help section at first:
help cat
% Or more exhaustive:
doc cat
You will see, that the first input must be the dimension, along which the list of folloing arrays is concatenated.
In your case cat(peaks) appears, before peaks is defined. This looks strange. I guess you want to replace:
Allpeaks=cat(peaks)
[peaks, locs]= findpeaks (y);
by
[peaks, locs]= findpeaks(y);
Allpeaks = cat(1, Allpeaks, peaks); % Or cat(2, ...) ?
  4 件のコメント
Walter Roberson
Walter Roberson 2021 年 12 月 17 日
編集済み: Walter Roberson 2021 年 12 月 17 日
allPeaks = zeros(10,10); %empty vector
That is not an empty vector.
allPeaks = peaks; %adding new peaks to end of vector
and that does not add anything to the end of anything.
findpeaks() typically finds a variable number of peaks, so you would typically expect a different length of peaks each iteration of k . Are you sure you want to put them into a vector? You would not be able to tell them apart.
Consider using a cell array
FieldNames = fieldnames(MCR_full.MIB037.Reaches);
allPeaks = cell(10,1);
for k = 1 : 10
thisFieldName = fieldNames{k};
thisArray = MCR_full.MIB037.Reaches.(thisFieldName).kin;
x = thisArray(:, 1);
y = thisArray(:, 3);
peaks = findpeaks(y);
allPeaks{k} = peaks(:).';
disp(size(peaks));
reachLimitArray= find (peaks >= 0.12);
if length(reachLimitArray )> 1
disp('There is at least two value above the limit.');
for i = 1 : length(reachLimitArray)
index = reachLimitArray(i);
disp(peaks(index));
end
else
disp('All values are below the limit.');
end
end
celldisp(allPeaks);
Mackenzie Maher
Mackenzie Maher 2021 年 12 月 17 日
Thank you so much you are wonderful it worked!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDescriptive Statistics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by