MATLAB Answers

What is the easiest way to embed a text data file into a .m function so that you don't need two files.

13 ビュー (過去 30 日間)
Zeb Barber
Zeb Barber 2018 年 11 月 1 日
コメント済み: Zeb Barber 2018 年 11 月 1 日
I have a function that uses a pre-generated lookup table that is stored as a text file. I would like to embed the text file into the .m file so that I don't have to move both files around.

  0 件のコメント

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

採用された回答

Guillaume
Guillaume 2018 年 11 月 1 日
編集済み: Guillaume 2018 年 11 月 1 日
Load your text file into a variable, however you normally read it in your code, for example:
lookuptable = fileread(yourtextfile);
Then right click on the variable in the variable browser, select 'Save As...' and in the 'Save as type' dropdown change the type from mat file to m file to let matlab autogenerate the code to create that variable.

  3 件のコメント

Zeb Barber
Zeb Barber 2018 年 11 月 1 日
This didn't work as it just generate code that accessed a .mat that has all the data in it. Still have the problem of having to cart around an extra .mat file instead of a .txt file.
Trying to get around having to format my lookup table into MATLAB code.
Guillaume
Guillaume 2018 年 11 月 1 日
Go into matlab preferences and under Workspace increase the Maximum array size threshold to something larger that the size of your variable. Above that threshold matlab indeed resort to saving the variable into a mat file for efficiency / readability.

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

その他の回答 (1 件)

Elias Gule
Elias Gule 2018 年 11 月 1 日
I guess you can store your data as part of the code. Your may create an inner function or a subfunctions that initializes your lookup table; you can also store your data as properties of a class.

  1 件のコメント

Zeb Barber
Zeb Barber 2018 年 11 月 1 日
I am trying to avoid having to write a function that reformats the text (300 lines long) into MATLAB code that can be pasted into an m-file. Really just trying to get around having to cart around an extra file to make the m-file work.

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

Community Treasure Hunt

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

Start Hunting!

Translated by