我想从文本文件中读取参数并将其汇总为四个变量。由于参数很多,我决定写一个for循环来自动化它。
问题是,循环只会创建一个名称错误的变量“ ans”而不是“ machnumber”。 这是我的代码,也许您可以看到任何故障:
function [machNumbers, weights, altitudes, specificRanges]=readsrm(myfilename)
myfilename = "parameterfile.txt"
fid = fopen(myfilename,'r');
fskipl(fid, 3);
# read number of machnumber
numMachNumber = fscanf(fid, "%i", 1)
fscanf(fid, "%s", 5);
machNumbers = fscanf(fid, "%f", numMachNumber);
# read weights
numWeights = fscanf(fid, "%i", 1);
fscanf(fid, "%s", 5);
weights = fscanf(fid, "%f", numWeights)
# rad altitudes
numFl = fscanf(fid, "%i", 1)
fscanf(fid, "%s", 5);
altitudes = fscanf(fid, "%f", numFl)
irrelevantValues = fscanf(fid, "%s", 7)
for altitudes = 1:numFl
for weights = 1:numWeights
specificRanges(altitudes, weights, 1:numMachNumber) = fscanf(fid, "%f", numMachNumber);
endfor
endfor
fclose(fid);
endfunction
这是文本文件:
SRMST 15112012 4
26 MN CV BI CV MACHNUMBER
0.44 0.46 0.48 0.50 0.52 0.54 0.56 0.58
0.60 0.62 0.64 0.66 0.68 0.70 0.71 0.72
0.73 0.74 0.75 0.76 0.77 0.78 0.79 0.80
0.81 0.82
29 KG LN BI LN WEIGHT
39000. 40500. 42000. 43500. 45000. 46500. 48000. 49500.
51000. 52500. 54000. 55500. 57000. 58500. 60000. 61500.
63000. 64500. 66000. 67500. 69000. 70500. 72000. 73500.
75000. 76500. 78000. 79500. 81000.
24 FT LN BI LN ALTITUDE
18000. 19000. 20000. 21000. 22000. 23000. 24000. 25000.
26000. 27000. 28000. 29000. 30000. 31000. 32000. 33000.
34000. 35000. 36000. 37000. 38000. 39000. 40000. 41000.
18096 NMTO LN BI LN SPECIFIC RANGE
176.47 175.17 176.48 173.93 170.98 167.27 163.47 159.51
155.33 151.18 147.02 142.67 138.02 133.25 131.67 131.67
我欢迎您的帮助。
祝一切顺利,
迈克尔