Reading a text file into a function as variables

2 ビュー (過去 30 日間)
Lily Muller
Lily Muller 2014 年 9 月 3 日
編集済み: Guillaume 2014 年 9 月 3 日
I have a file with 6 columns of numerical data, tab delimited:
Eg. 1 2 3 4 5 6
7 8 9 10 11 12
13 14 15 16 17 18
etc.....
I have a function called gpoly which requires the following inputs:
g=gpoly(x0,z0,xcorn,zcorn,ncorn,rho)
Where all these inputs are numbers. I would like to run this function using each line of my data file as these 6 inputs each time to produce a list of g's at the end.
Could someone please explain how would be best to do this? I have tried readtable and rowfun which did not work. Possibly a for loop iterating over each line, but I am not sure of the syntax in Matlab?
Many thanks, Lily

採用された回答

Guillaume
Guillaume 2014 年 9 月 3 日
編集済み: Guillaume 2014 年 9 月 3 日
For completeness:
D = dlmread(somefile);
Then, if gpoly works on arrays:
args = num2cell(D, 1);
g = gpoly(args{:});
If it only works with scalar:
g = zeros(1, size(D, 1)); %edited for silly mistake
for row = 1:size(D, 1)
args = num2cell(D(row, :));
g(row) = gpoly(args{:});
end
  2 件のコメント
Lily Muller
Lily Muller 2014 年 9 月 3 日
Many thanks for the answers. I have tested and gpoly can only work on scalars so I have tried using the code you provided:
g = zeros(1, row);
for row = 1:size(D, 1)
args = num2cell(D(row, :));
g(row) = gpoly(args{:});
end
However I get an error in the following line:
g = zeros(1, row);
Error: Undefined function or variable 'row'.
Thanks, Lily
Guillaume
Guillaume 2014 年 9 月 3 日
編集済み: Guillaume 2014 年 9 月 3 日
yes, sorry made a mistake. Editing the answer now.
Or you could just have ignored the line, that just predeclare g.

サインインしてコメントする。

その他の回答 (1 件)

Iain
Iain 2014 年 9 月 3 日
dlmread
That's a function that is designed to read delimited files into matlab. - It'll likely give you an array with all the data in it, ready to use.
You could try using the import wizard instead though.

カテゴリ

Help Center および File ExchangeData Import and Export についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by