Search All of the Math Forum:
Views expressed in these public forums are not endorsed by
NCTM or The Math Forum.


Phil
Posts:
61
Registered:
12/8/08


Re: Save strings and numbers together
Posted:
Mar 14, 2013 8:14 AM


"Phil Roberts" wrote in message <khse66$h8a$1@newscl01ah.mathworks.com>... > "Bruce " <blmckinley@signalprocessing.com> wrote in message <khprk6$c5d$1@newscl01ah.mathworks.com>... > > "Phil Roberts" wrote in message <khpl27$nat$1@newscl01ah.mathworks.com>... > > > Hi, > > > > > > I have the following strings and number as separate variables. How can I save them together in one file, such as a .txt file? > > > > > > Strings > > > 'Test Point' > > > 'speed_Engine_av' > > > 'torque_Brake_av' > > > 'power_Brake_av' > > > > > > Numbers > > > 1 > > > 2 > > > 3 > > > 4 > > > > > > I wish to save them so that the string and its corresponding number are on the same row i.e. Test Point 1, speed_Engine_av 2 etc > > > > > > Thanks in advance, > > > > > > Phil > > > > As long as the strings don't include any commas, you can use a Comma Separated Value file. You can open the file as a spreadsheet and the cells will appear in separate columns. Or you can open it and fscanf your values back into MATLAB or using any other programming language. > > > > strvar = {'Test Point', 'speed_Engine_av'. 'torque_Brake_av', 'power_Brake_av'}; > > val = {1, 2, 3, 4}; > > fid=fopen('myfile.csv','wt'); > > for i=1:length(val) > > fprintf(fid,'%s, %d\n',strvar{i},val{i}); > > end > > fclose(fid); > > %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% > > Thanks for the response. > > If I have I have an additional column of numbers in the "val" parameter, how to I add this to fprintf so both columns of val are printed in the file? I am a new user of matlab so apologies for the simipicity of this question. > > Thanks in advance, > > Phil
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Sorry, I have sorted my question above.
However, I have another question.
How do I increase the number of output columns in fprintf if "val" is changing each time? For example: For 1 column in val; fprintf(fid,'%s %5.3f\n',strvar{i},val(i,:)) For 2 column in val; fprintf(fid,'%s %5.3f %5.3f\n',strvar{i},val(i,:)) For 3 column in val; fprintf(fid,'%s %5.3f %5.3f %5.3f\n',strvar{i},val(i,:))
I would like to add the %5.3f for each additional column in val automatically.
Many thanks in advance,
Phil



