How do I format adding columns to a matrix in a textfile or in general
2 次查看(过去 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).
采纳的回答
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!