Semi colons aren't suppressing output
3 views (last 30 days)
Show older comments
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 Comments
Stephen23
on 10 Jun 2022
"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
on 13 Jun 2022
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.
Accepted Answer
Jan
on 13 Jun 2022
Edited: Jan
on 13 Jun 2022
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 Comments
More Answers (0)
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!