How do I format adding columns to a matrix in a textfile or in general

1 次查看(过去 30 天)
So I have a matrix that I am printing to a file that looks like this
1, 2, 3;
6, 7, 8;
11,12,13;
I want to add a two column vectors of the form
[4 ;9 ;14] and [5 ;10 ;15]
to make the updated file look like
1, 2, 3, 4, 5;
6, 7, 8, 9,10;
11,12,13,14,15;
I am running a while loop to get the column vectors so I either have to update the matrix i the while loop before printing it, I dont know the code syntax to do that, or update the matrix in the file after each while loop, this seems easier to place in my code, but I don't know the syntax on how to code this. If anyone could offer help on this issue I would appreciate it. (I dont know how to format matrices in this Q&A so I apologize if this question is hard to look at).
  4 个评论
kollin poindexrer
kollin poindexrer 2017-6-26
The property of a csv to start writing to the file at a designated column and row should help a lot. I will give it a shot thanks.
kollin poindexrer
kollin poindexrer 2017-6-26
I tried using csv to write the file but it ends up like [,,,,5 ;,,,,10 ;,,,,15].
It feels like starting at the 3rd column and 0th row makes everything prior into commas even though there is information there. I am using cvswrite(A,0,p+2), where A is the column vector I want to insert and p is a value in the for loop that should make the file start writing after the last column
(for example columns 0-2 in the file were already written in, so p(starts at 1) +2 makes the program start writing at the 3rd column. But for some reason it rewrites the matrix to have commas for the 0-2 columns.)

请先登录,再进行评论。

采纳的回答

Jess Lovering
Jess Lovering 2017-6-26
If you wanted to append the file in that manner over each loop then you would have to read in each line, add to the line, and write to a new file - not a very efficient way to do it. I don't think that you can just add to the end of each line simply like you would like to do. You would be better off adding the data to your matrix in the loop and writing the file at the end. You can use the cat function to concatenate the new array to the main matrix pretty easily. Type "doc cat" at your command prompt to pull up the help on this.

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by