Need to load a file into a variable

6 ビュー (過去 30 日間)
Andy
Andy 2012 年 1 月 26 日
This is what i have:
f = load (filename)
where filename is just a string of characters (e.g. load_me.mat)
when i load this, i have to do f.load_me to access the information i want. Is there a way for me to just type in "f" and access all the information i loaded? Thanks
  1 件のコメント
Andy
Andy 2012 年 1 月 26 日
i forgot to mention, i want to do this because everytime this program is ran, the filename to be loaded may have a different name

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

採用された回答

Walter Roberson
Walter Roberson 2012 年 1 月 26 日
If the file only has a single numeric variable in it, then when you save() the file, save it with the -ASCII option to a text file. When you load() a text file, no variable names are introduced.
When you load() a .mat file and assign the output to a variable, then the variable assigned to will always be a structure with one field for every variable that was stored in the file, even in the case where you ask to load() only one variable.
You can make an assignment such as:
fn = fieldnames(f);
f = f.(fn{1});
and then f would refer to the content of whatever the first variable in the .mat file happened to be. This would not, however, strictly satisfy the requirement to 'just type in "f" and access all the information i loaded' as it requires typing in two other commands as well.
Ah, it appears it could be shortened to one command:
f = cell2mat(struct2cell(f));
  1 件のコメント
Andy
Andy 2012 年 1 月 26 日
perfect, thanks

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSystem Commands についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by