Prev: china
Next: automatic gain control
From: dpb on 27 Jun 2010 09:18 Martin wrote: .... > I gather the trick is to combine SPRINTF with some kind of print > function like FPRINTF? > Can I do that even for a square matrix output? What does the matrix size/shape have to do w/ the filename? Or there's another question/uncertainty here that isn't asked??? If the question is to write a square matrix in a text file, then yes, you could use fprintf w/ the appropriate formatting. doc fprintf shows writing a two-column array; it shouldn't take much to see where/how to change that to write as many columns as desired per line/row. --
From: dpb on 27 Jun 2010 09:32 dpb wrote: .... > doc fprintf > > shows writing a two-column array; it shouldn't take much to see > where/how to change that to write as many columns as desired per line/row. > OBTW, study the order of the vector/array in the example; don't just pass over the generation step. Remember Matlab stores arrays/matrices in column-major order and the vectorized xprintf/xscanf functions operate in that sequence. If your square matrix is stored as otherwise, you'll need the transpose of it in the argument of the fprintf call... Compare the result of the example if the data were given as x = [0:.1:1]'; y = [x; exp(x)]; instead of as the example is written to see the effect. --
From: Steven Lord on 27 Jun 2010 22:01 "Martin " <nikolom(a)gmail.com> wrote in message news:i05smu$8f8$1(a)fred.mathworks.com... >I did > > for k=1:9 > load(sprintf('/data/tot_00%d.dat',k)); > end > > which worked. Once I increase the number of files k above 9, I have > problems. The file names are tot_001 through tot_999. How do I format the > tot_%d so I can read 001 through 999? While you can do this, I would use the DIR approach to look for the files whose names match the pattern tot_*.dat. -- Steve Lord slord(a)mathworks.com comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ To contact Technical Support use the Contact Us link on http://www.mathworks.com
From: Martin on 28 Jun 2010 10:14 "Steven Lord" <slord(a)mathworks.com> wrote in message <i08vqa$hr2$1(a)fred.mathworks.com>... > > "Martin " <nikolom(a)gmail.com> wrote in message > news:i05smu$8f8$1(a)fred.mathworks.com... > >I did > > > > for k=1:9 > > load(sprintf('/data/tot_00%d.dat',k)); > > end > > > > which worked. Once I increase the number of files k above 9, I have > > problems. The file names are tot_001 through tot_999. How do I format the > > tot_%d so I can read 001 through 999? > > While you can do this, I would use the DIR approach to look for the files > whose names match the pattern tot_*.dat. > > -- > Steve Lord > slord(a)mathworks.com > comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ > To contact Technical Support use the Contact Us link on > http://www.mathworks.com > Thanks, Steve. What function or approach would you use to write to different output files? I plan to run a for loop with the index incrementing for each new read file, do my work on the file (256x256 matrix), and then output it to a new output file for a given index number. I have 999 input and therefore will generate 999 output files. Would I use the sprintf('/data/tot_00%d.dat',k)) method to assign the index number to the output files? Can it even work with, say, fprintf function? I think I am looking for an output version of the load function where I can hide the index number of the file. I know this could be something simple but I an not too experienced with Matlab and I am grateful for any help.
From: dpb on 28 Jun 2010 11:06
Martin wrote: .... > Thanks, Steve. What function or approach would you use to write to > different output files? I plan to run a for loop with the index > incrementing for each new read file, do my work on the file (256x256 > matrix), and then output it to a new output file for a given index > number. I have 999 input and therefore will generate 999 output files. > Would I use the sprintf('/data/tot_00%d.dat',k)) method to assign the > index number to the output files? Can it even work with, say, fprintf > function? I think I am looking for an output version of the load > function where I can hide the index number of the file. Steve said "don't do that"... :) I gave an example just above of creating a file name based on the input name; you can "salt to suit" however you want to modify the name but the example I gave will have the numeric value of the input file already embedded in it. As for the use of sprintf() and its usage w/ some other function, you're again "failing to generalize" a principle. A character string like a file name that an example piece of code shows as being hard coded can be substituted for by a variable containing a string at any point in Matlab syntax. Or, of course, the results of a function w/o an intermediate store to a variable such as sprintf() is simply a character string reference as well. Study the example I gave in your other thread on the same subject (as corrected by Steve L as I did neglect the detail of needing the functional form for save). I'd even suggest taking the semicolon off the [p,name,ext] = fileparts(fname); statement and to add newf = fullname(p [name '_A' ext]) in the dir() loop to see what it is outputting. Since you're using load(), apparently, the corresponding routine that goes with it is save() save( fullname(p [name '_A' ext]) 'A' ) Again, as noted, you _can_ use sprintf() and/or friends, and you may ultimately decide to do so for the output file but look at the above and see if something built on it doesn't suit; particularly for the input side as Steve and others have suggested. -- > > I know this could be something simple but I an not too experienced with > Matlab and I am grateful for any help. |