Loop: Index Exceeds Matrix Dimensions / less values than required.
1 次查看(过去 30 天)
显示 更早的评论
Hello Everyone,
How can settle the following code so that It may run properly.
clear;
clc;
load('Dataset.mat'); % length of dataset is 189.
start_limit = 1; % initialization
end_limit = 6; % initialization
Final = struct(); % structure
limit = (length(Dataset))/6; % I want to have a division of 6. i.e. 189/6 = 31.5
for i = 1:limit % Note: Length of Dataset is 189
Values = Dataset(start_limit:end_limit,13); % runs best till 186, since 186 is wholy divisble by 6.
FieldName = strcat('Name', num2str(i));
Final.(FieldName) = cell2mat(Values);
start_limit = start_limit+6;
end_limit = end_limit+6;
end
Code runs till 186 but i want to have a values till 189. When i add +1 to limit, i got following error:
Index exceeds matrix dimensions.
Error in Untitled (line 35)
Values = Dataset(start_limit:end_limit,13);
I understand the error but how to overcome this?
Any help would be appreciated :-)
Regards,
Waqar Ali Memon
0 个评论
采纳的回答
Rik
2019-7-23
If it is fine to use fewer values for the end of your loop, you can do this:
%replace this
Dataset(start_limit:end_limit,13)
%by this
Dataset(start_limit:min(end_limit,end),13)
However, you should probably not be using a struct with numbered fields. I would suggest a struct array instead.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!