Please help: ' Dimensions of arrays being concatenated are not consistent'
3 views (last 30 days)
Show older comments
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 Comment
Yazan
on 14 Aug 2021
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?
Accepted Answer
Yazan
on 14 Aug 2021
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)'});
More Answers (0)
See Also
Categories
Find more on Tables 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!