Hi everyone. I want to create a shell script which will identify a text file given from the user and then open matlab and extract data from within the text file. This is what I've done so far:
#!/bin/bash
read -p "Type desired file for data mining :" filename
echo "Selected file : $filename.txt"
cd Desktop/ocean_lab_data
vi $filename.txt
cd ../
cd ../
cd ../
cd ../
cd usr/local/MATLAB/R2016b/bin/
./matlab -nosplash -nodesktop
addpath(genpath('directory of the text file'));
A=importdata('$filename.txt');
My problem for now is that I can't pass the commands following "./matlab -no splash -nodesktop" into matlab. Any help will be much appreciated.

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 30 Jul. 2017

0 Stimmen

Use the -r flag
./matlab -nosplash -nodesktop -r "addpath(genpath('directory of the text file')); A=importdata('$filename.txt');"

7 Kommentare

Walter thank you for your answer. When I ran the script it couldn't open the selected file. I suppose matlab can't recognize the variable "filename". What should be done?
You have not used a fully-qualified filename, but you cd'd to a directory where the file does not exist.
#!/bin/bash
alias matlab="/usr/local/MATLAB/R2016b/bin/matlab"
datadir="~/Desktop/ocean_lab_data"
read -p "Type desired file for data mining :" filename
ffilename="$datadir/$filename.txt"
echo "Selected file : $ffilename"
vi "$ffilename"
matlab -nosplash -nodesktop -r "addpath(genpath('$datadir'));
A=importdata('$ffilename');"
Paschalis Garouniatis
Paschalis Garouniatis am 31 Jul. 2017
Bearbeitet: Paschalis Garouniatis am 31 Jul. 2017
I ran it and ended with an error stating that matlab command not found. Does the last line need "./"? Also I had to change ~/Desktop/ocean_lab_data with the full path because it opened an empty file.
You would not use ./matlab on that last line.
On my system I have,
alias matlab="/Applications/MATLAB_R2017a.app/bin/matlab -nojvm -nodesktop"
after which I can just use
matlab
at the shell prompt.
In any case you can rewrite without using alias:
#!/bin/bash
matlab="/usr/local/MATLAB/R2016b/bin/matlab"
datadir="~/Desktop/ocean_lab_data"
read -p "Type desired file for data mining :" filename
ffilename="$datadir/$filename.txt"
echo "Selected file : $ffilename"
vi "$ffilename"
"$matlab" -nosplash -nodesktop -r "addpath(genpath('$datadir'));
A=importdata('$ffilename');"
except you might need to tune datadir as you indicated.
I modified the alias like yours on your system and it works. In the last line when you call the alias "matlab" the rest of the command line "-r ..." is not recognized and ends up with the same error as before. Is there a solution to that? I will also try it without the alias as you commented.
I tried it without the alias and it worked. Thanks a lot for your time Walter. If you have any suggestion for my previous comment please post it. Thnx again.
At the moment I cannot think of any reason why additional arguments would be ignored if alias was used.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Startup and Shutdown finden Sie in Hilfe-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