How to generate a code that automatically calls/uses the Import Tool in Matlab for a text file with variables that change occasionally?

1 view (last 30 days)
Hey Everyone,
I use the Import Data tool in Matlab 2015b to import a text file occasionally to create plots with the variables that are created. I would create a function using the 'generate function' option, but the variables in the text file change occasionally. At the moment, I have to use the Import Data tool all the time and have to paste the 'generate script' into my active plotting script.
I would like to automate the process that can handle text files whose headers (variables) always change. The biggest issue I find is auto-generating the 'formatSpec' which Import Data tool does well somehow. Is it possible to select a text file, and have Matlab automatically generate formatSpec?
Any suggestions?
Thanks,
MG
  2 Comments
Looky
Looky on 10 Nov 2017
Can you give an example file? What kind of changes do you expect from file to file?
There is the importdata function, that is rather sophisticated and does a good job dealing with unknown formats of your data. It imports the data nicely. If that's all what you want, it should do the trick.
If that doesn't work, or you explicitly need the formatSpec you might be able to generate one from the assumptions how similar your files are.
Does maybe the header contain what kind of data is stored in a column(numeric, date, time, string)?
Stephen23
Stephen23 on 10 Nov 2017
@MG: as long as the file has a regular structure and is well delimited then it is easy to automatically read the header names and the data columns, and construct a structure from these. Then by accessing the fieldnames it is trivial to check what data is in the file, or plot the desired data.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!