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 CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by