필터 지우기
필터 지우기

Creating data structures in loop for later reference, loop is deleting all but last entry.

조회 수: 2 (최근 30일)
I am attempting to make a script which will pull NURBS data from Blender text file, I have managed to extract the control points for the generation of the structure. But, I need it to perform the generation automatically. I made a loop which runs the function and puts the result into a cell array, that way I call later call on it as " crv{1},crv{2}" etc. and for graphing purposes.
However, each time I run the script, it will only keep the last loop results and result in my "crv" cell array saying "1x7" but then listing the results as " [] [] [] [] [] [] [1x1 struc]" if I run it for 7 loops, the data is properly recorded in the last entry but all of the others are empty.
Unless you have the NURBS/Geopdes package installed, running the code yourself will not work.
I would like to know if their is a better way to create structures in a loop than this, I know dynamically creating variables is a huge no-no, but I can't think of what else to do if the cell array method doesn't work.
<<
>>
close all; clear; clc;
content = fileread( 'TEST_CUBE.txt' ) ;
expr = '[^\n]*v [^\n]*';
matches = regexp(content,expr,'match');
n = numel(matches);
for i = 1:n
expression = 'v ';
replace = '';
matches(i) = regexprep(matches(i),expression,replace);
end
for ii = 1:n
for i = ii
C = cell2mat(matches(i));
end
NUM = str2num(C);
Ctrl{i} = NUM;
end
for i = (n-1)
crv{i} = nrbline([Ctrl{1,i}(1,1) Ctrl{1,i}(1,2) Ctrl{1,i}(1,3)],[Ctrl{1,i+1}(1,1) Ctrl{1,i+1}(1,2) Ctrl{1,i+1}(1,3)]);
nrbctrlplot(crv{i});hold on;
end

채택된 답변

Walter Roberson
Walter Roberson 2018년 1월 28일
Are you sure you want
for i = (n-1)
which executes only for the single value (n-1) ? Perhaps you want 1:n-1 ?
  댓글 수: 1
Garrett
Garrett 2018년 1월 28일
편집: Garrett 2018년 1월 28일
That is so stupid of me. Such a simple little typo. This is embarrassing.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by