How to pass file from Bash script to MATLAB script

I have a bash script that creates a config file of varying name and then runs a Matlab script through the following:
(config file production)
matlab -nodesktop -nodisplay -r "MatScript; exit"
How do I set MatScript to read/load the config file that is produced, since the name can vary. Basically I want to pass this file from my bash script to my Matlab script, so then Matscript can work with the values in it. Thanks!

 Accepted Answer

Make your script into a function:
matlab -nodesktop -nodisplay -r "try; MatScript('$CONFIGLOCATION'); end; exit"
Or assign to a variable
matlab -nodesktop -nodisplay -r "try; CONFIGLOCATION='$CONFIGLOCATION'; MatScript; end; exit"

6 Comments

Great thank you! In the Matlab script, how would I open up/load the config file then?
Depends on what you expect to be in it.
What I posted above assumes that you have set the environment variable CONFIGLOCATION to the name of the file, such as
CONFIGLOCATION=$(mktemp)
echo > "$CONFIGLOCATION" <<EOF
Step_what: Reactor Power Input
How_much: 3 more points
EOF
But idk, perhaps your config file is in the form of MATLAB script to be run(), or perhaps it is in the form of an INI file
g
g on 15 Nov 2018
Edited: g on 15 Nov 2018
So I used the option where it's set as a variable, and I put file = '$outfile' for my respective code. In the matlab script, I put disp(file), and it showed me my file, so I know it's being passed properly. As for loading it, I'm not sure how to do this. I tried load(file) and load(file,'-mat') and tried displaying variables in my file but it doesn't seem to work. Perhaps there is some nuance I'm missing with regard to how variables are set when loading the file? Thanks.
Is it a mat file? Did you try
filestruct = load(file); disp(fieldnames(filestruct))
It’s not a mat file. I can try that though. Basically the file is a .cfg file that has several variables in it like so:
a=2
b=75
c=apple
and I want to load the file and use the variables in some calculations.
if it is in matlab syntax then use fileread to read the file as text and then use
try
evalc(TheContent);
catch ME
end
The evalc prevents all those assignments from displaying results . The try prevents you from crashing on syntax error .
But might I suggest that you use the INI parser that I posted the link to and pull fields out of the struct? That would be safer than risking the cfg file overwriting one of your variables .
If cc=apple is intended to create a string you will need parsing code for sure .

Sign in to comment.

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Tags

Asked:

g
g
on 15 Nov 2018

Edited:

on 18 Nov 2018

Community Treasure Hunt

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

Start Hunting!