Creating .m file with variables to charge in another script

10 Ansichten (letzte 30 Tage)
Hello there
I was wondering if there is a way to create a .m file I could charge in my script just to keep the script more readable. I've multiple arrays of structures I would like to just add to my other script. I know I could save into a .mat file but this would mean I would need to always run my .m file, then save to a .mat file at the end of the day an example of the structs I would have is down below.
firstData(1).example = 1;
firstData(1).foo = 2;
firstData(1).thirdThing = 3;
firstData(2).example = 4;
firstData(2).foo = 5;
firstData(2).thirdThing = 6;

Akzeptierte Antwort

Guilherme Theis
Guilherme Theis am 1 Apr. 2020
To do what I want I only need to call the file on the second script.
data script (data.m)
firstData(1).example = 1;
firstData(1).foo = 2;
firstData(1).thirdThing = 3;
firstData(2).example = 4;
firstData(2).foo = 5;
firstData(2).thirdThing = 6;
Second script:
data
myEquation = firstData(1).example/4;

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 1 Apr. 2020
Yes. You can fopen() a .m file. If you are going to just add more lines to it without reading the existing lines, then use 'a' permission. If you want to be able to read the existing file and add more lines to it, then use 'a+' permission. If you want to be able to read and write the file at arbitrary locations inside the file (not just appending) then use 'r+' permission.
If you opened with 'a' or 'a+' then any fwrite() or fprintf() you do to the file should automatically move to the end of the file, but you will need to fseek() to the position you want to read from. If you opened with 'r+', then you will need to use fseek() to move to the position that you want to read from, and you will need to fseek to the position you want to write to, even if you want to write immediately after reading.
Remember to fclose() the file.
There is a trick involved: if the .m file contains a function, use "clear" and the function name, or else the parser might not notice the changes. It probably would not hurt to do the same thing with a script file.
  2 Kommentare
Guilherme Theis
Guilherme Theis am 1 Apr. 2020
What I want is then do something like:
fopen('myfile.m')
someDivision = firstData(1).example/5;
which doesn't seem to be the case
Walter Roberson
Walter Roberson am 1 Apr. 2020
fopen() of a file returns a file identifier to use to do file input and output such as with fscanf() or fprintf()
If you have a .m file you want to execute then you can just name the basic file name (without the directory or .m) if it is on your path, or use run() possibly with a directory and definitely including the .m , such as run('myfile.m')
someDivision = firstData(1).example/5;
Note that if you execute a script, that altering a variable mentioned in the script does not change the script. For example,
firstData(1).example = 305;
would not change the script. If you want to change the script, then you would use file I/O operations with it... but really a lot of the time it is easier to fileread() the file as a character vector, manipulate the vector, and then use fopen() / fwrite() / fclose() to create a new copy of the file. Updating a text file in place gets messy.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Low-Level File I/O finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by