how to call some columns and fprintf ???

i have text file contains 50 columns and 50 rows , for example how i want print column 2 4 5 45

Réponses (1)

dpb
dpb le 2 Mar 2014
Modifié(e) : dpb le 2 Mar 2014
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 commentaires

dpb
dpb le 3 Mar 2014
Modifié(e) : dpb le 4 Mar 2014
Well, then, no I don't understand the question.
I showed you exactly how to write a selected set of columns which is the question you asked. If you want a single row at a time in a loop, that's very inefficient unless the array is so large as to have memory issues but the method is the same excepting for where you put the fprintf statement and how you address the row index/indices desired.
...
OK, maybe it's the "... to print the columns 2 4 5 45 with other 45 to be [50*50]" that's the question?
That comes under the heading of "salt to suit" for the format string...
fmt=[repmat('%.3f ',1,length(icol)) '[%dx%d]\n'];
fprintf(fid,fmt,[x(:,icol) repmat(size(x),size(x,1),1].')
Image Analyst
Image Analyst le 6 Mar 2014
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
dpb le 6 Mar 2014
Who knows???? Seems to have gone away and to be rather inconsiderate of those trying to help, anyway...

Connectez-vous pour commenter.

Catégories

En savoir plus sur Scripts dans Centre d'aide et File Exchange

Commenté :

dpb
le 6 Mar 2014

Community Treasure Hunt

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

Start Hunting!

Translated by