For large arrays, Matrix rows carry onto next line theyre written in from a fortran program
42 views (last 30 days)
Show older comments
I am running a fortran program which operates over quite a large gridspace and when i write it to a matlab file it carries some of the values that are meant to be on the same row onto the next line which then means not all rows are the same length. When i decrease the gridspace so that all values of each row stay on the same line it works fine however i need to run it with a larger gridspace.
I have tried to add "..." to each line but to do this for such a large matrix seems like an inefficient solution. I tried to find if a whole section of code could be treated with one "..." but couldnt find anything on it. I have also tried writing to a .dat file which i will then read into matlab but this had lead to further complications.
Any suggestions are appreciated.
Thank you.
1 Comment
Accepted Answer
Jan
on 4 Apr 2022
Storing large blocks of data as source code is a bad idea. Text or binary files are much better.
Matlab tries to be smart and let you omit the separators for elements and rows:
A = [1 2 3
4 5 6
7 8 9]
This is not save and as soon as operators are inserted, it get amginuous:
[1 2], [1 - 2], [1 -2], [1-2]
The clean way is to write explicitly, what you need:
A = [1, 2, 3; ...
4, 5, 6; ...
7, 8, 9]
B = [1, -2];
1 Comment
Daksh
on 19 Dec 2022
Kindly save your data in .txt or binary files. As for writing your own data, make sure to use proper punctuations between values (like commas, braces) for data clarity and separation, similar to Jan's answer.
Hope it helps!
More Answers (0)
See Also
Categories
Find more on Logical 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!