converting data in Excel CSV to specific format in text

1 view (last 30 days)
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:
  1. Read data from Excel-CSV (almost 600 rows and 2 columns)
  2. 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
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->' ?
GS
GS on 14 May 2015
Hello Walters, thanks for your response. Here is my reply for your queries 1. I have used '<' and '-' symbols just to represent 8 characters reservation needed for each cell of excel-CSV in text. In the final file, I dont want it to be represented. It should be only numbers related to cell Ai and Bi of CSV file otherwise remaining characters of 8 (in text) should be left empty. 2. It doesn't matter to me if the numbers are left aligned or right aligned within 8 characters reservation. The text format I want is related to Nastran FEA software (used while creating tabled ID)

Sign in to comment.

Accepted Answer

Joseph Cheng
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
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.
GS
GS on 15 May 2015
Thank you Joseph. Your time was valuable to me and gave an hint to proceed further. I had gone through the manual and for a beginner it is always a question if the existing code can still be optimized by a specialist like you. As you know there are many ways to write codes. I will try to do it and get back for any questions if I have. Thanks once again.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!