Please help: ' Dimensions of arrays being concatenated are not consistent'
2 次查看(过去 30 天)
显示 更早的评论
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
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
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 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!