Semi colons aren't suppressing output
3 次查看(过去 30 天)
显示 更早的评论
I have this as part of my code, i try to suppress as the output takes a while to finish, but semi colons aren't working
SECTIONS={'Joints','Model','Segments','Trajectories'}; % the looked for data sections
ich=find(cellfun(@ischar,G(:,1))); % the records that have char() data first column
isec=ich(contains(G(ich,1),SECTIONS)); % the locations of each section beginning
isec=[isec;size(G,1)+2]; % add last line for indexing sections first:last lines
for i=1:numel(SECTIONS);
vn=string(G(isec(i)+3,:));
in=find(ismissing(vn));
vn(in)="Var"+in;
cmd=sprintf('t%s=cell2table(G(isec(%d)+5:isec(%d)-2,:))',SECTIONS{i},i,i+1);
eval(cmd);
end
4 个评论
Stephen23
2022-6-10
"where do you mean?"
At the end of the text that you EVAL: is there a semi-colon? Answer: no.
"There is a semi colon on the end of each line"
Sure, but that is not the problem.
"or do i need it elsewhere?"
Yes, at the end of the text that you provide to EVAL.
Note that using a structure or cell array would be simpler, more reliable, and more efficient than using EVAL:
Peter Perkins
2022-6-13
I mean, the bigger thing is, "don't do that". You are creating vars in your workspace using sprintf and eval. There are lots of posts about why that is not a good thing.
That whole line of code, with the exception of the LHS of the assignment, can be written as literal code, using i as a subscript. And if you assign to a field of a scalar struct as mydata.(['t' SECTIONS{i}]), you can get rid of the eval.
采纳的回答
Jan
2022-6-13
编辑:Jan
2022-6-13
The problemis here:
cmd =sprintf('t%s=cell2table(G(isec(%d)+5:isec(%d)-2,:));', SECTIONS{i}, i, i+1);
% ^ insterted
A much better way to store the data is:
t.(SECTIONS{i}) = cell2table(G(isec(i)+5:isec(i+1)-2,:));
Creating variables dynamically impedes the processing speed, because the JIT acceleration cannot know in advance, which variables are existing and which are not. Then instead of replacing calls of variables by fast pointers, Matlab search each variable in a lookup table - even the ones to concerned by the EvAL command. This can slow down the code by a factor of 100.
In addition EVALing variables makes it hard to impossible to debug your code. You see, that a forgotton semivolon is hard to find already. Compare the readability of the two lines above.
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!