How can I import .m files in a sequential way with Editor?

1 view (last 30 days)
Hi everyone.
I'm trying to make automatic the process of opening some files but I'm stuck.
I used to open them manually in the Command Window typing it's name. (e.g. If I want to open a file called vort050hgrid.m, I type vort050hgrid ).
I want to make a .m file in the editor that opens other vort0XXhgrid.m files in a sequential way.
I started creating a for structure to create the name of the file, but I don't know how to open it. It looks like this:
for i=50:25:150
str= num2str(['vort0' num2str(i) 'hgrid'])
%%here is where i need your help
end
If you need some more information, I can say that they contain data looking like:
data050=[
1.47734139218924,1.25,-0.00865578969217656,0.271968032236597
2.36824211711376,1.25,-0.00865578969217656,0.265838179532844
2.36824211711376,1.25,-0.00865578969217656,0.259708326829091
3.14539483375015,1.25,-0.00865578969217656,0.253578474125338
.
.
.
Thank you all. Pablo.

Accepted Answer

Stephen23
Stephen23 on 25 Feb 2015
Edited: Stephen23 on 25 Feb 2015
It is very unusual to store data in a callable Mfile script. In MATLAB, like most programming languages, data and the executable code are considered to be two different paradigms and are not usually stored together or handled in the same way. This helps make code more robust, by allowing us to create code that is generalized to a reasonable degree and not specific to one set of data. It also allow us to change the data without having to change the code itself.
It would be more usual to store simple numeric data in something like a .CSV file, some kind of binary file or a perhaps a .MAT file, and import this into MATLAB using the usual file-reading functions.
For example you might have some numeric and image data from an experiment: these would normally be kept in the simplest storage form possible, and only read into MATLAB when required. You can see that making the numeric data a callable script or function is rather unbalanced, as the image data would likely not get treated like this.
In your case you should redefine them as a simple .CSV files that would look like this:
1.47734139218924,1.25,-0.00865578969217656,0.271968032236597
2.36824211711376,1.25,-0.00865578969217656,0.265838179532844
2.36824211711376,1.25,-0.00865578969217656,0.259708326829091
3.14539483375015,1.25,-0.00865578969217656,0.253578474125338
...
There are many tools for reading data into MATLAB (see the link I gave above), but the obvious choice for this data would be to use textscan. To see how to read multiple such data files in a loop, have look at the wiki:
  1 Comment
Pablo Martínez Filgueira
Pablo Martínez Filgueira on 27 Feb 2015
Thanks for your answer. I did what you said about using .csv files (my CFD software exports in .csv) and everything has been easier. Your explaination about how to work has been brilliant.
The last link has been very useful for me to make a sequence of files.
Thank you! ;)

Sign in to comment.

More Answers (1)

Adam
Adam on 25 Feb 2015
Edited: Adam on 25 Feb 2015
open( str );
should do the trick.
  1 Comment
Pablo Martínez Filgueira
Pablo Martínez Filgueira on 27 Feb 2015
That wasn't what I wanted to do, as I tried it before. Anyway thanks for your quick answer ;) .

Sign in to comment.

Categories

Find more on Large Files and Big Data in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!