MATLAB Answers

What is wrong with this FORTRAN to MATLAB conversion?

4 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

Show 2 older comments
Star Strider
Star Strider on 8 Jul 2016
It would be easier to help if you attached your data file here. Use the ‘paperclip’ icon.
Kenneth Lamury
Kenneth Lamury on 9 Jul 2016
Here is my input:
*PROJECT_TITLE++05--06__07--08__09--10__11--12__13--14__15--16__17--18__19--20__FORMAT(20A4)
10 2.433 241.88 786.65 312.82 312.82 0 7 0 0 0****----****----****----FORMAT(6F7.0,4I3,I2,6A4)
605.24 747.31839.3325618.8 122.79 34.142 .7688 .49444 0. 0. 1 FORMAT(10F7)
550.15 668.65339.3325565.5 144.37 34.142 .7688 .49444 0. 0. 2 "
495.070589.98839.3325512.1 165.96034.142 .7688 .49444 550.761412.839ON GEOM"
439.99 511.32339.3325461.8 187.54 31.58 .7688 .49444 0. 0. 4 "
384.91 432.65839.3325413.5 209.13 31.58 .7688 .49444 0. 0. 5 "
330.280353.99339.3325365.10 232.50 31.58 .7688 .47083 320.769240.82 IN GEOM
275.64 275.32839.3325316.7 255.94 31.58 .7688 .47083 0. 0. 7
221.04 196.66339.3325275.0 279.38 28.5 .7688 .47083 0. 0. 8
166.43 117.99839.3325240.1 302.82 22.17 .7688 .47083 130.1 312.82 MG GEOM
111.81 39.332539.3325214.1 326.26 10.73 0. .0001 0. 0. 10
.40 .175439.096154.044444.024938.014749.008726.005051.002857.002439EI405 R
.50 .250 .121951.057143.035587.021692.013333.008 .004484.004515GJ405 R
3 4 16.0 3.9961 1 0 9 0 0 0 0KENG
.5 0 0 THRPIT°
1.3240 0.000 0 0 0 0 0 MP THR
0000000000000000000000000000000000000000000000000000000000000000000000VZ THR IAP
-.24360-.23560-.22750-.47400-.42390-.32570-.14540MEA THR IAP
.38200 .38200 .38200 .77420 .79860 .84200 .88720TEA THR IAP
0 0 0 -.99966-.99966-.99966-1.9994-1.9994-1.9994-1.9994VX THR IAP
0000000000000000000000000000000000000000000000000000000000000000000000VY THR IAP
0 0 0 -.04063-.11809-.19555-.32029-.47554-.63111-.78712MZ THR IAP
201.0 * TMI
-9.0 -8.0 -7.0 -6.0 -5.0 -4.0 -2.0 0.0 4.0 6.0 * TAL
8.0 10.0 11.0 12.0 13.0 14.0 15.0 16.0 17.0 18.0 * TAL
.245 .164 .106 .072 .049 .032 .014 .01 .009 .009 M=1.0 TCD"
.01 .014 .021 .032 .049 .072 .106 .163 .244 .341 M=1.0 TCD"
-126.67-40.141-.24780 206.06 62.339 .38190 .85191 .0 0.0.523689O-NAC.JUMPFORMAT(10F7.0)
*____OUTB'D NAC_____* FORMAT(21A1,7F7.0)
-126.67-57.292-.25450 206.06 88.291 .39220 .85191 0 0.0.523689I-NAC.JUMP IAP
*_____INB'D NAC_____*
-97.783 -4.613.000000221.234 10.438.914640.000000 0 0.0.404269* MAINGEAR"
*-MAIN GEAR UP LOAD-* "
1.0 -8073 .53526-.84557552.590 -8271 .69555-1.0828323.360 1183.9FLT-UP KCRFORMAT(10F7.0)
.03261-.10898136.270 continued reading
2.0 -8073 .53526-.84557552.590 -8271 .69555-1.0828323.360-3579.0FLT-DN KCR IAP
-.15468 .35382136.270
3.0 -8230 .54673-.87494552.590 -8281 .69639-1.0841323.360-3579.0GRD-DN KCR IAP
-.15468 .35382136.270
0000000 FORMAT(10F7.0)
0000000 continued reading
1 4 4 -1 .910 .810 .730 .702 0 0KC-R ONACFORMAT(4I7,6F7.0)
-17.154-22.425-26.642-28.453 XHEAFORMAT(10F7.0)
4 7 6 -1 .640 .600 .545 .480 .460 .411KC-R INAC
-34.327-38.117-43.329-49.487-51.387-56.025 XHEA
7 9 5 -1 .330 .300 .270 .200 .173 0-135 GEAR
-63.699-64.877-64.510-62.982-61.132 XHEA
9 11 2 1 .120 .090 0 0 0 0-135 SOB
-59.216-56.298 XHEA
3 10 .399 1 1 3 6 MCR,NACLDSFORMAT(10F7.0)
5.01 6.02 6.47 6.56 7.00 6.3 6.7 7.4 7.05 7.45 M0 "
.0170 .017 .0284 .0120 .0064 .0244 .0072 .0060 .0154 .019 ALPHA
-.0248 -.026 -.0272 -.0269 -.0242 -.0217 -.0203 -.0197 -.0207 -.0223 CM0
.03 .007 .003 -.003 .001 -.005 -.013 -.031 -.075 -.112 DCMDCL
.4 M=.4
5.4362 6.5438 7.0163 7.4000 7.4100 6.8 7.17 7.9505 7.6549 8.1114 M0
.017 .0167 .0281 .0122 .0060 .0235 .0138 .0034 .0148 .018 ALPHA
-.0274 -.0281 -.0293 -.0291 -.0261 -.0235 -.0216 -.0211 -.0224 -.0242 CM0
.0284 .0073 .0039 .0039 .0014 -.0025 -.01 -.0301 -.0768 -.1126 DCMDCL
.55 M=.55
5.65 6.9713 7.4656 7.615 8.6684 7.6 8.45 8.7944 8.1911 8.6294 M0
.0184 .0159 .0275 .0126 .0183 .0245 .0207 .015 .0144 .0190 ALPHA
-.0283 -.0297 -.0306 -.0302 -.0275 -.026 -.0244 -.0223 -.0237 -.0256 CM0
.0314 .0073 .0054 .0091 .0049 -.001 -.0107 -.0276 -.0764 -.1126 DCMDCL
.7 M=.7
6.512 7.421 8.2154 9.2263 8.6588 7.3 7.8 8.4646 8.6729 9.3123 M0
.0288 .0212 .0305 .0092 .0236 .035 .0195 .0187 .0209 .0219 ALPHA
-.0308 -.0328 -.0354 -.0363 -.0321 -.0286 -.0234 -.0228 -.026 -.0277 CM0
.0366 .0176 .0163 .022 .0135 -.001 -.0095 -.025 -.0741 -.1131 DCMDCL
.8 M=.8
7.0820 8.3683 8.6008 8.8377 8.2319 7.5 8.6 9.635 10.050010.6633 M0
.0151 .017 .0336 .0227 .0258 .0355 .0275 .0176 .0174 .0173 ALPHA
-.0374 -.0378 -.0377 -.0375 -.0347 -.0325 -.0272 -.0263 -.0292 -.0311 CM0
.03 .011 .0027 -.0034 .0032 -.002 -.0095 -.0191 -.0741 -.1153 DCMDCL
.85 M=.85
7.8582 9.3157 9.5266 9.3 8.7588 7.95 7.9 8.5366 9.962 11.0504 M0
.0085 .0144 .0311 .0246 .0209 .027 .017 .0093 .0169 .0194 ALPHA
-.0379 -.0338 -.0437 -.0496 -.0431 -.0374 -.029 -.0250 -.03 -.0361 CM0
.0652 .0020 .0324 .0591 .0344 -.0075 -.047 -.0819 -.1574 -.2066 DCMDCL
.9 M=.9
7.540811.042210.1302 7.0963 8.6429 7.632 8.0 8.7013 9.545910.4801 M0
.0 .013 .031 .035 .0176 .0292 .0221 .0127 .0107 .0135 ALPHA
-.0476 -.0410 -.0453 -.0544 -.0398 -.043 -.0305 -.0233 -.0227 -.0274 CM0
.125 -.0126 .0215 .0693 -.0225 -.06 -.092 -.142 -.2049 -.2508 DCMDCL
.95 M=.95
8.5713 7.0532 6.9459 6.6719 6.915 6.9 6.9 7.4086 9.703310.7462 M0
.0193 .0248 .0414 .003 .0073 .0198 .031 .0249 .0097 .0081 ALPHA
-.06 -.0490 -.0526 -.0564 -.0294 -.0384 -.0315 -.0218 -.0138 -.0115 CM0
.0913 .0843 .0814 .0608 -.0858 -.1498 -.1954 -.2034 -.2603 -.3058 DCMDCL
BLANK
-.0014 -.0008 -.0153 -.0187 -.0063 -.0207 -.012 -.0064 -.0052 -.0046 SPOILER
-.0009 .00123 .0014 -.00085-.00438 .0058 .00295 .00297 .0063 .00838 INPUTS
0000000 2ND CONTRO"
0000000 SURFACE"
.10 -.00200 .0665-.00100 .0292-.00340 .0716-.00140 .0292 0LAOA 1 FORMAT(10F7.0)
0 0 0 continued reading
.40 -.00160 .0647-.00074 .0289-.00362 .0603-.00147 .0395 0LAOA 2
0 0 0
.55 -.00145 .0653-.00070 .0301-.00377 .0669-.00144 .0375 0LAOA 3
0 0 0
.70 -.00150 .0676-.00074 .0305-.00430 .0693-.00149 .0378 0LAOA 4
0 0 0
.75 -.00157 .0682-.00078 .0298-.00460 .0790-.00160 .0358 0LAOA 5
0 0 0
.80 -.00180 .0676-.00086 .0292-.00510 .1040-.00166 .0339 0LAOA 6
0 0 0
.825 -.00195 .0670-.00092 .0290-.00535 .0960-.00171 .0322 0LAOA 7
0 0 0
.85 -.00220 .0665-.00100 .0289-.00565 .0954-.00177 .0299 0LAOA 8
0 0 0
.875 -.00255 .0653-.00106 .0287-.00600 .0877-.00183 .0306 0L0AO 9
0 0 0
.90 -.00300 .0650-.00120 .0285-.00640 .0888-.00189 .0302 0LAOA 10
0 0 0
.925 -.00370 .0642-.00120 .0284-.00670 .0802-.00195 .0292 0LAOA 11
0 0 0
.95 -.00500 .0630-.00135 .0281-.00710 .0698-.00200 .0265 0LAOA 12 "
0 0 0 "
BLANKFORMAT(10F7.0)
BLANKcontinued reading
.10 .0096 .00214-.04800 .00480 CFM-BIV6FORMAT(10F7.0)
.30 .0096 .00214-.04800 .00480 CFM-BIV6 2
.40 .0068 .00214-.04800 .00480 CFM-BIV6 3
.55 .0007 .00209-.04691 .00383 CFM-BIV6 4
.70 -.0092 .00190-.04590 .00258 CFM-BIV6 5
.80 -.0198 .00230-.04701 .00100 CFM-BIV6 6
.85 -.0145 .00294-.05611 .00437 CFM-BIV6 7
.90 -.0176 .00340-.05900 .00739 CFM-BIV6 8
.95 -.0244 .00270-.03344 .00307 CFM-BIV6" 9
BLANKFORMAT(10F7.0)
1 1 2 1 FORMAT(10I7)
.00144 .00733 .02125-.80198-.78749-.76392-1.8507-1.6503-.933901.14687TSTRRE UPFORMAT(10F7.0)
-.00258-.04264-.12665-.15142-1.1779-2.3085-3.4050-5.6952-8.1845-10.639MSTRRE UP"
-.1570 -.5960-1.2910-10.400-11.626-13.203-23.331-25.474-27.490-31.124VSTRRE UP IAP
5642 25468 27628 4219 6.5 62.957FLT RE IAP
7500 250 .4343 1.0000 1.0000 0.0 0.0 735.798395 0
260.0 .267 1.0 0 .3571 0 0 0 0 62.957
28.30 0 1 1 0 0
1 1 0 0 0 0 0 0 0
260.0 .267 1.5 0 .3571 0 0 0 0 62.957
28.35 0 1 1 0 0
1 1 0 0 0 0 0 0 0
-1 GW<0
30000 280 .7760 1.0000 1.0000 0.0 0.0 735.374730 0
260.0 .267 1.0 0 .3626 0 0 0 0 62.957
58.30 0 1 1 0 0
1 1 0 0 0 0 0 0 0
260.0 .267 1.5 0 .3626 0 0 0 0 62.957
58.35 0 1 1 0 0
1 1 0 0 0 0 0 0 0
-1 GW<0
-1 0 VE=0
-1 EI(1)<0 "
-1 -1 FINAL FORMAT(10F7.0)
END or EOF
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

Show 3 older comments
Star Strider
Star Strider on 7 Jul 2016
I don’t have your data, so I’m guessing here.
I would do the textscan call as:
XYc = textscan(fid, fmt_num1, 1, 'CollectOutput',1);
XY = cell2mat(XYc);
and then refer to the appropriate columns (or rows) of ‘XY’ as you need them?
Kenneth Lamury
Kenneth Lamury on 8 Jul 2016
Thanks y'all, but the program still does not work.
The first usage of 'textscan' of '%80c' followed by 'strjoin' joins the 80 characters into one 'string' TITLE.
The second usage of 'textscan' followed by equating the dimensioned 10 '%7f' fields to a single variable works.
The third usage of 'textscan' in the 'for' loop still does not work as it reads and equates the dimension-ed variables to their proper fields only on the first looping. The following loops (it stops after 6 out of n loops) produced miscellaneous values and NaN's, and [0x10 double] matrices with the error message: XYc =
[0x10 double]
Index exceeds matrix dimensions.
Error in CNSMTX (line 49) XV(i)=XY(1)
If I cannot solve reading variables from a fixed 10 x '%7f' field I will not be able to read 4 x X1(i),X2(i),X3(i),X4(i) as i equals 1 to N, which will require reading two lines when N>3 or L>10. Nearly all of the readings after the third 'textscan' will be 1 row x L reads, which will be converted to into L x 1 column variables.
I do not have a FORTRAN compiler, only the source code. Thus, the desire to rewrite the FORTRAN program into a MATLAB version.
I'm semi-retired with little to do, but I want to continue learning for fun and activities.

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

Show 1 older comment
Walter Roberson
Walter Roberson on 8 Jul 2016
You are being affected by the problem I have mentioned multiple times now: that the counts do not start until a non-blank. Because of this your reading is getting out of synchronization with your data. Your %24c is reading into the second line and is happening to leave the pointer positioned in a place that cannot match a %7f format because the 7 characters happens to include a blank.
Getting textscan to not skip leading blanks even with a %c or %[] specifier is tricky; to do it you need to set the 'Whitespace' option to '' (the empty string).
Take your numeric format and remove all of the blanks, because at the very least they lead the reader to expect blanks between fields. So not '%7f %3f' but '%7f%3f'
Then change all of the numeric format specifiers to %c format specifiers. So not '%7f%3f' but '%7c%3c' . And add 'Whitespace', '' to the textscan calls.
Then each place you go to extract numeric data from the cell array, use str2double(cellstr(XY{2})) (for example) on the entry. Notice the {} to extract the content of the entry and notice the cellstr to convert it from a char array to a cell array of strings to feed to str2double. However, if you only extracted from a single line then you can skip the cellstr() stage, such as str2double(XY{2})
Each place that you go to extract text data from the cell array, remember that when you use %c in textscan then the cell you get back will be a single char array, not a cell array of strings like %s or %[] would give you. Chances are that you are going to want to cellstr() the result before using it, if you extracted from multiple lines.
Kenneth Lamury
Kenneth Lamury on 10 Jul 2016
This solution works for fixed field formats from FORTRAN. It ended the dreaded cursor stops which picks up bleed-ed data.
fid=fopen('ZLS918_919_KCR_Test_Up_O.txt','r');
fmt_num0=['%7f%7f%7f%7f%7f%7f%3f%3f%3f%3f%2f%24c'];
fmt_num1='%7f%7f%7f%7f%7f%7f%7f%7f%7f%7f';
Title=textscan(fid,'%80c',1);%reads 1st line of input data
ProjectTitle=strjoin(Title); %Conjoins 80 columns into 1 line of data
disp(ProjectTitle) %Displays conjoined columns without single quotes
fgetl(fid); %Get a new line of data
XXc=textscan(fid,fmt_num0,1); %Scan the line per format
XX=cell2mat(XXc) %MATLAB converts 10 cells to 10 numerics per format
XXL=XX(1);
L=XXL %Converts floating point to integer in FORTRAN
SW=XX(2);
CBAR=XX(3);
B2=XX(4);
XMAC=XX(5);
XREF=XX(6);
S1CON=XX(7);
PUNCH=XX(8);
IPROP=XX(9);
IDUM=XX(10);
ITNGO=XX(11); %Although integer in FORTRAN it will be used appropriately
ITIT=XX(12); %Displays conjoined title
for i=1:L
%2345678901234567890123456789012345678901234567890123456789012345678901234567890
fgetl(fid);
XYc=textscan(fid,fmt_num1,1);
XY=cell2mat(XYc);
XV(i)=XY(1);
YV(i)=XY(2);
H(i)=XY(3);
XEA(i)=XY(4);
C(i)=XY(5);
EAANGL(i)=XY(6);
TANL(i)=XY(7);
TANT(i)=XY(8);
YE(i)=XY(9);
XE(i)=XY(10);
end
fclose('all'); %Close all open files after reading them
By using fgetl, textscan & cell2mat I do not have to use Adriaan Van Nuffel's fixed_width_import.m function which is set to 10F7.0 format.
Thanks everyone who gave assistance.
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!

Translated by