how to call some columns and fprintf ???
5 次查看(过去 30 天)
显示 更早的评论
i have text file contains 50 columns and 50 rows , for example how i want print column 2 4 5 45
0 个评论
回答(1 个)
dpb
2014-3-2
编辑:dpb
2014-3-2
This essentially identical to the previous question I answered...
i want print column 2 4 5 45
Given array x of 50x50, say, and
icol=[2 4 5 45];
fmt=[repmat('%.3f ',1,length(icol)) '\n'];
fprintf(fid,fmt,x(:,icol).')
...
Again, salt to suit for formatting, etc., ...
ADDENDUM:
Unless the question is simply one of how to reference a subset of an array in which case the answer is embedded in the above by use of the predefined column index vector. This is basic Matlab syntax; if you don't understand that, go to the "Getting Started" section and work thru the basic addressing exercises.
NB: the line you've commented as giving an error is owing to the bad syntax that attempted to store the colon operator in the d vector. That may seem like a reasonable thing to try to do, but it is simply not supported by Matlab syntax.
d=(:,[2 4 5 45]); % you can't do this--invalid syntax w/ the 'colon'
Use the form demonstrated above instead.
NB2: You can't use
irow=[2 23 92];
icol=[2 4 5 45];
z=x(irow,icol);
however, despite it looking ok and being a desirable thing to do. In that case you'll have to use the two vectors as arguments to sub2ind. See the documentation for details on the whys and hows of that.
3 个评论
Image Analyst
2014-3-6
dpb, it seems like he deleted a comment before yours. All that's there now is his "original" question "i have text file contains 50 columns and 50 rows , for example how i want print column 2 4 5 45", which actually I'm not so sure now is original.
dpb
2014-3-6
Who knows???? Seems to have gone away and to be rather inconsiderate of those trying to help, anyway...
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!