Loading text files using variable names

2 ビュー (過去 30 日間)
Kingsley Ripley
Kingsley Ripley 2011 年 4 月 26 日
Hi, Is there any way of loading a text file (say mydata.txt) using a variable name? What I need to do is allow the user to input the name of a text file, which contains an array, in the previous command, and then for the program to load the file with name and store it into another variable. Thanks in advance.

採用された回答

Arnaud Miege
Arnaud Miege 2011 年 4 月 26 日
filename = input('Enter the file name (without the txt extension)','s');
fid = fopen([filename '.txt']);
C = textscan(fid, <format to be used>);
fclose(fid);
HTH,
Arnaud
  2 件のコメント
Kingsley Ripley
Kingsley Ripley 2011 年 4 月 26 日
At the risk of sounding stupid, what conversion specifier would I then use to load a 3x3 numeric array?
Arnaud Miege
Arnaud Miege 2011 年 4 月 26 日
It depends on what data type it is and how the data is delimited. Assuming a simple text file with:
5.2 5.3 5.4
6.5 6.6 6.7
7.8 7.9 8.0
You'll need something like:
C = textscan(fid,'%f %f %f','CollectOutput',1);
HTH,
Arnaud

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Type Identification についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by