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:
fid=fopen('ZLS918_919_KCR_Test_Up_0.txt','r');
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)
%READ(20,106) XXL,SW,CBAR,B2,XMAC,XREF,S1CON,PUNCH,IDUM,IDUN,ITNGO,
% & (ITIT(I),I=1,6)
ProjectTitle=textscan(fid,'%4c',20); %FORMAT(20A4)
[XXL,SW,CBAR,B2,XMAC,XREF,S1CON,PUNCH,IPROP,IDUM,ITNGO,...
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), ...
YE(i),XE(i)]=textscan(fid,fmt_num1,1)
end
Error using textscan Too many output arguments.
Error in CNSMTX (line 13) ITIT]=textscan(fid,fmt_num0,1)
  5 Comments
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.
  6 Comments

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 http://www.mathworks.com/matlabcentral/answers/293547-can-one-read-one-line-of-string-title-followed-by-multiple-lines-of-numeric-data-like-fortran#comment_376916: 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 http://www.mathworks.com/matlabcentral/fileexchange/10866-fixed-width-import 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()
  4 Comments
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!