Please help: ' Dimensions of arrays being concatenated are not consistent'

1 回表示 (過去 30 日間)
Tomaszzz
Tomaszzz 2021 年 8 月 13 日
コメント済み: Tomaszzz 2021 年 8 月 14 日
Hi all,
I am trying to extract variable name 'average pressure(kpA)' and its each respective numeric value from the total lenght of the attached data and structure it in a table.
When I run the code (below and attached) I get the following error:
Error using vertcat. Dimensions of arrays being concatenated are not consistent.
It spits out the table (t) but with only the first 15 values that I am interested in . Each time when I run the loop manually, it spits out 15 more.
I cannot find the solution how to fix it. Could you please help?
Many thanks
The code
d = load('Data.mat');
n = size(d.data_raw2, 1);
%% Find the left heel keyword
idx_lheel_start = find(strcmp(d.data_raw2(:, 2), '"Left Heel"'));
idx_lheel_end = find(strcmp(d.data_raw2(:, 2), '"Right Heel"'));
t = table();
for i=1:length(idx_lheel_start)
idx = find(strcmp(d.data_raw2(idx_lheel_start(i):idx_lheel_end(i), 1), 'Average Pressure (kPa)'));
a.AVR_PRESSURE = d.data_raw2{idx_lheel_start(i)-1+idx, 2};
t = [t; struct2table(a)];
end
  1 件のコメント
Yazan
Yazan 2021 年 8 月 14 日
That's an extremely inefficient way to extract the average pressure(kpA) values in a table! Why don't you just do it directly through logical indexing without looping?

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

採用された回答

Yazan
Yazan 2021 年 8 月 14 日
clc, clear
d = load('Data.mat');
idx_lheel_start = find(strcmp(d.data_raw2(:, 2), '"Left Heel"'));
idx_lheel_end = find(strcmp(d.data_raw2(:, 2), '"Right Heel"'));
% indices between idx_lheel_start and idx_lheel_end
idx = arrayfun(@(idxStart, idxEnd) idxStart:idxEnd, idx_lheel_start, idx_lheel_end,...
'UniformOutput', false);
% corresponding Average Pressure
avgP = cellfun(@(idxx) d.data_raw2(idxx(strcmp(d.data_raw2(idxx, 1), ...
'Average Pressure (kPa)')), 2), idx);
% save in table
T = table(str2double(avgP), 'VariableNames', {'Average Pressure (kPa)'});

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by