converting data in Excel CSV to specific format in text
1 view (last 30 days)
Show older comments
Hello all, I am trying to create a matlab script. I am successful in CSV to text conversion and adding headers. But getting stuck to print the text file derived from Excel-CSV in below mentioned format
Objective of the script:
- Read data from Excel-CSV (almost 600 rows and 2 columns)
- Store in text file with specific format.
Specific format: (restriction/reservation of 8 Characters for each cell of CSV data)
<--+---><--A1--><--B1--><--A2--><--B2--><--A3--><--B3--><--A4--><--B4-->
Next line
<--+---><--A5--><--B5--><--A6--><--B6--><--A7--><--B7--><--A8--><--B8-->
Next line
<--+---><--A9--><--B9--><--A10-><--B10-><--A11-><--B11-><--A12-><--B12->
-
-
-
to be continued till end of file.
request your help
2 Comments
Walter Roberson
on 13 May 2015
Are the '<' and '-' and '>' literal? Will the entries always be exactly 2 or 3 characters? Would '<----A1>' be an acceptable alternative form for '<--A1-->' ? Would '<-A10-->' be an acceptable alternative form for '<--A10->' ?
Accepted Answer
Joseph Cheng
on 14 May 2015
so... i'm curious what if an excel-csv is different from the first csv file you wrote. Either way the methods to import/read from csv or excel is extensively documented and it looks like you don't have any trouble. to write it in the format that you want you can do something like:
x=randi(100,12,2)
nx = x';
fprintf(' +%8.0f%8.0f%8.0f%8.0f%8.0f%8.0f%8.0f%8.0f\n',nx)
which gives the output of
+ 88 63 28 58 21 6 57 94
+ 65 73 42 74 21 7 95 87
+ 9 94 11 99 15 86 17 79
I rarely write to files using fprintf so there might be a cleaner way but if you read the documentation for fprintf you can see that you can pad the numbers you write as well left justify. I transposed the test matrix x (which should be your imported 200x2 csv data) since the fprintf goes by columns by rows. Here i didn't include the file ID so it prints directly into the command window.
2 Comments
Joseph Cheng
on 14 May 2015
Edited: Joseph Cheng
on 14 May 2015
I'll only help if you edit your code input with the {}code button also i don't have much more time to keep on this question so don't hold your breath waiting for a response.
Best thing to do is fully read the documentation on fprintf and understand what is going on when you do make a change and not just make changes. when you say it didn't succeed figure out why it didn't succeed based on your inputs. if you listed what you tried and why it doesn't do what you want someone here can explain it better and what to change. Otherwise it is just someone else writing code for you.
More Answers (0)
See Also
Categories
Find more on Text Data Preparation 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!