Hello, i clicked through the answers, but it didnt help.
What am i trying to do? Put for each round of the loop two values in file. Overall 9 rounds, two values each -> 18 values
What have i done? - I've created two 9x6 matrices called xwerte and ywerte
- I've created two arrays called xd_ref and yd_ref
Script looks like this:
% Loop goes trough each row
for j=1:length(xwerte)
% loope goes through each column of specific row
for i=1:length(xwerte(j,:))
% Simple function that gives out two values and puts it into file
[S, ab] = dist_pt2refTraj(xwerte(j,i), ywerte(j,i), xd_ref, yd_ref);
save('trajektorien.mat', 'S', 'ab');
if ab > 20
error('Error. \n ab is to big')
end
end
end
If i click on run, i get in my file only two values, not 18.
How can i debug through each loop? Or whats the problem here?
Thanks a lot.

 採用された回答

infinity
infinity 2019 年 6 月 24 日

0 投票

Hello,
In your code, you save variables "S" and "ab" with the same name for each loop. Therefore, the 'trajektorien.mat' only stores the value of the last loop.
To avoid this problem you should save "S" and "ab" for each loop to a vector. Then just save the vector. Because of depending on how do you want to store data, below could be a possible solution
saveS_ab = [];
% Loop goes trough each row
for j=1:length(xwerte)
% loope goes through each column of specific row
for i=1:length(xwerte(j,:))
% Simple function that gives out two values and puts it into file
[S, ab] = dist_pt2refTraj(xwerte(j,i), ywerte(j,i), xd_ref, yd_ref);
saveS_ab = [saveS_ab; S ab];
save('trajektorien.mat', 'saveS_ab');
if ab > 20
error('Error. \n ab is to big')
end
end
end
Now, you data is store in varibale "saveS_ab"
Check does it work or not.

4 件のコメント

Daniel Wurster
Daniel Wurster 2019 年 6 月 25 日
Good answer, thanks! I tried it and got a file with 54x2 double in it. Not 9x2 double. I think it depends on the function that calculates. Try to figure that out now.
Jan
Jan 2019 年 6 月 25 日
編集済み: Jan 2019 年 6 月 25 日
@Trung VO DUY: Currently the file is overwritten in each iteration. It is more efficient to write the file once only:
saveS_ab = [];
for j = 1:size(xwerte, 1) % Much safer than length(xwerte) !
for i = 1:size(xwerte, 2)
[S, ab] = dist_pt2refTraj(xwerte(j,i), ywerte(j,i), xd_ref, yd_ref);
saveS_ab = [saveS_ab; S ab];
if ab > 20
% Maybe a SAVE here also?
error('Error. \n ab is to big')
end
end
end
save('trajektorien.mat', 'saveS_ab');
In addition length(x(1, :) is replaced by the direct size(x, 2), because this avoids to create the temporary vector x(1, :) only to get its length.
A pre-allocation of the output would be even better: Use zeros to set the variable saveS_ab to the final value:
saveS_ab = zeros(size(xwerte));
for j = 1:size(xwerte, 1) % Much safer than length(xwerte) !
for i = 1:size(xwerte, 2)
[S(j,i), ab(j,i)] = dist_pt2refTraj( ...
xwerte(j,i), ywerte(j,i), xd_ref, yd_ref);
if ab(j,i) > 20
% Maybe a SAVE here also?
error('Error. \n ab is to big')
end
...
Daniel Wurster
Daniel Wurster 2019 年 6 月 25 日
Hello Jan, thanks for the answer. Whats the reason to pre-allocation the output in the same size as xwerte?
If i click on run i get:
error using vertcat
Dimension of arrays being concatenated are not consistent.
Error in (Filename) (line27)
saveS_ab = [saveS_ab; S ab];
If i click on run and changed saveS_ab = zeros(size(xwerte)); to saveS_ab = [];
i get no error.
infinity
infinity 2019 年 6 月 25 日
Thank @Jan.
These sugguestions will optimize the code and save time for the case of big data.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

質問済み:

2019 年 6 月 24 日

コメント済み:

2019 年 6 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by