MATLAB Answers

What is wrong with this FORTRAN to MATLAB conversion?

2 views (last 30 days)
I'm running MATLAB R2016a Student and getting error messages. Here is the CNSMTX program:
fmt_num0='%7f %7f %7f %7f %7f %7f %3f %3f %3f %3f %2f %24c';%FORMAT(6F7.0,4I3,I2,6A4)
fmt_num1='%7f %7f %7f %7f %7f %7f %7f %7f %7f %7f'; %FORMAT(10F7.0)
fmt_num2='%21c %7f %7f %7f %7f %7f %7f %7f'; %FORMAT(3A4,7F7.0)
% & (ITIT(I),I=1,6)
ProjectTitle=textscan(fid,'%4c',20); %FORMAT(20A4)
ITIT]=textscan(fid,fmt_num0,1) % line 13 in MATLAB
%READ(20,101) (XV(I),YV(I),H(I),XEA(I),C(I),EAANGL(I),TANL(I),
% 1 TANT(I),YE(I),XE(I),I=1,L)
for i=1:XXL
[XV(i),YV(i),H(i),XEA(i),C(i),EAANGL(i),TANL(i),TANT(i), ...
Error using textscan Too many output arguments.
Error in CNSMTX (line 13) ITIT]=textscan(fid,fmt_num0,1)
Walter Roberson
Walter Roberson on 9 Jul 2016
Instead of copying the file in as text, please use the paperclip to attach the file so we can download it.

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 6 Jul 2016
The textscan function outputs one cell of cell arrays for each field in the format specifier, or one a cell of cell arrays for each class (numeric and character) if you specify 'CollectOutput',true. See the documentation for details, since my description here lacks them.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 7 Jul 2016
Edited: Walter Roberson on 8 Jul 2016
I repeat the concerns from field counts do not start until the first non-blank. The format you are using with '%7f %7f %7f' and so on implies that you are sure that you have a blank between fields and that the fields are definitely present and that the occupied portion of them is the exact width you indicate. If there are spaces between the fields and the fields are present for sure, then just use uncounted fields '%f%f%f' and so on . If there are not always spaces between fields then Yes you need a counted field, but to get that to work right you also need to have the case that the field is always fully occupied.
My previous response referred you to and I recommend you take a look at that.
The strategy I would use for fixed width fields that might not have spaces and might be partly occupied (typical Fortran output) would be to split the line into substrings of appropriate size (perhaps using mat2cell, since you can specify the number of columns for each cell), and then take a pass through the result doing sscanf() with uncounted format (because leading blanks) or just using str2double()
Walter Roberson
Walter Roberson on 10 Jul 2016
The cell2mat(XXc) is going to fail. You can only cell2mat() if everything has the same data type, but you have the %21c at the end of the numerics.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!