How to add input parameters for matlab application execution - matlab

I'm writing a script in MatLab and I need to execute a program (written in C) as one of the lines (it generates an output file).
My current code is:
!collect2.exe infile.csv <-- I want to be able to change this to a variable but I can't
My question is, is there a way for me to either:
A. put a variable in place of infile.csv such as
!collect2.exe filedir
or
B. run multiple files without a variable
Thanks in advance :)
Edit:
filedir = input('What is the directory with quotes?');
!cd /
cmdString = ['cd ', filedir];
system(cmdString);
Edit #2:
Never mind, I fixed the issue. Thanks for all of your help!

Use the system function.
Example:
filename = 'infile.csv';
cmdString = ['collect2.exe ', filename];
system(cmdString);

you can create a variable called filename, per your example it would be
filename='infile.csv';
or alternately have you tried using this to launch multiple files at once?
!collect.exe {infile.csv,infile1.csv,infile2.csv};

Related

Execute script but don't touch workspace in Matlab

Can I execute script, but make it doesn't touch any current workspace variables? I.e. turn script into function?
Also it would be good to have access this script's variables after run.
I tried
evalin('myworkspace', 'myscript')
but it failed.
I tried
evalin('caller', 'myscript')
but it changed variables.
Is it possible to accomplish?
The trivial way to accomplish this without changing the script itself would be to:
save
myscript
% ... examine variables
clear
load
save saves the current workspace to a MAT-file called "matlab.mat". You can give it a different name if you prefer. load loads it in again. If you specified a different name for save, give the same name to load.
If you have the parallel computing toolbox, you can do it by submitting the script to your local cluster. (Just to emphasise, your local cluster is your own pc.)
E.g. if you have a script SO.m with the line
a = randn;
then you can submit and wait using
job = batch('SO', 'Profile', 'local');
wait(job);
Once it have finished running, you can load the variables using
M = load(job);
then all the variables are fields of M.
>> M.a
ans =
0.4010

What is the full command for gdal_calc in ipython?

I've been trying to use raster calculation in ipython for a tif file I have uploaded, but I'm unable to find the whole code for the function. I keep finding examples such as below, but am unsure how to use this.
gdal_calc.py -A input.tif --outfile=result.tif --calc="A*(A>0)" --NoDataValue=0
I then tried another process by assigning sections, however this still doesn't work (code below)
a = '/iPythonData/cstone/prec_7.tif'
outfile = '/iPythonData/cstone/prec_result.tif'
expr = 'A<125'
gdal_calc.py -A=a --outfile=outfile --calc='expr' --NoDataValue=0
It keeps coming up with can't assign to operator. Can someone please help with the whole code.
Looking at the source code for gdal_calc.py, the file is only about 300 lines. Here is a link to that file.
https://raw.githubusercontent.com/OSGeo/gdal/trunk/gdal/swig/python/scripts/gdal_calc.py
The punchline is that they just create an OptionParser object in main and pass it to the doit() method (Line 63). You could generate the same OptionParser instance based on the same arguments you pass to it via the command-line and call their doit method directly.
That said, a system call is perfectly valid per #thomas-k. This is only if you really want to stay in the Python environment.

How can I change the name of the file being saved without editing the code? MatLab

I am working on an experiment that take a lot of data samples and save it to different video files. For example, I take data and save it to a file called "slowmotion.avi" and then I take another set of data and save it to another file called "normalspeed.avi".
I am trying to find a way that I could change the name of file being saved without editing the code. The way I am using right now makes me have to open the code and change the name of the file directory within the code and then save the code. I want to make it a lot faster and easier to use.
I have tried the following code to fix this problem but it doesn't work.
graph=input('Graph of experiment: ');
vidObj = VideoWriter('%3.1f.avi\n',graph);
.....
Hope I didn't confuse you.
A possible solution:
graph=input('Graph of experiment: ','s');
vidObj = VideoWriter([graph,'.avi']);
The 's' in the input() function indicates that the expected input is a string, and [graph,'.avi'] simply concatenates both strings.

Function to convert relative paths to absolute paths?

I've spent quite some time to no avail looking for a built-in MATLAB function to convert relative file paths to absolute file paths (portably).
Is there one?
I'm looking for something preferably "built-in" (i.e. available somewhere in the MATLAB distribution, including one of its toolboxes). Even a "package-private" function would be acceptable, as long as I can examine the source code of function. Second best would be a third-party function, as long as it comes with a decent test suite. I am not looking for a function written in response to this question.1
Absent any of the above, even a function to test (portably) whether a path is absolute or not would do (with the same conditions as before: either a "built-in" function or a third-party function with a test suite).
1 The difficulty with implementing such a function is not writing the function itself, but rather writing a sufficiently complete test suite for it (and, of course, making sure that the function passes all the tests!).
fullfile(pwd, relative_path) converts a relative to a absolute path.
You can test if a path is absolute using
javaFileObj = java.io.File(pathToBeTested);
javaFileObj.isAbsolute()
Unlike char(javaFileObj.getCanonicalPath()), which indeed sometimes incorrectly returns a non-existent path relative to C:\Windows\System32, isAbsolute() seems to work properly (tested on Win7, MATLAB 2015b) Therefore the code for constructing the absolute path would look like
function F = rel2abs(F)
if ~java.io.File(F).isAbsolute
F = fullfile(pwd,F);
end
This function has the advantage of being idempotent.
The fullfile(pwd, relative_path) hack works well for me, but if you want something to get you the canonical form, there is no built-in (as of 2015b), but there is a well regarded downloadable script.
http://www.mathworks.com/matlabcentral/fileexchange/28249-getfullpath
See if which fulfills your requirements:
full_path = which(relative_path);
OK, let's resurrect an old thread, if anyone is looking for this.
Here is a method, if you want the absolute path relative to your current working directory (or pwd).
% pwd: 'C:\first\branch'
dir('.\').folder % returns same as pwd, 'C:\first\branch'
dir('..\').folder % returns path one level up from pwd, 'C:\first'
dir('..\parallel_branch').folder % returns the absolute path to "parallel_branch" folder next to pwd, 'C:\first\parallel_branch'
Here's the method that MathWorks themselves uses:
[status, info] = fileattrib(file);
if status
% Return the full path if fileattrib found the file.
fullFilePath = info.Name;
end
For someone landing here with slightly relaxed portability requirements (or a suitable test bench), another idea is to create a folder listing containing only a single file using the built-in dir function, followed by path concatenation using fullfile.
dirListing = dir(relPath);
absPath = fullfile(dirListing(1).folder, dirListing(1).name);
Pros:
Idempotent, i.e. rel2abs(rel2abs(path)) = rel2abs(path)
Simplifies out any /. and /.. in the relative path
Cons:
This only works if the file exists
Tested using Matlab R2020a on Windows

Moodle: How to set default blocks and their order in a new course?

I want to change default blocks and also order of them when a new course is created. I guess this should be done through editing source code, but if there is a way in application layer, that would be great!
I don't want to send my task to others. Finding out:
What is the proper way;
Which files should be checked;
What is the systematic way of doing it: through code, database or application.
is ok!
You can add following config variables in your config.php file according to your course format setting. In this setting colon is provided to separate the left and right blocks.
$CFG->defaultblocks_site = 'site_main_menu,course_list:course_summary,calendar_month';
$CFG->defaultblocks_social = 'participants,search_forums,calendar_month,calendar_upcoming,social_activities,recent_activity,course_list';
$CFG->defaultblocks_topics = 'participants,activity_modules,search_forums,course_list:news_items,calendar_upcoming,recent_activity';
$CFG->defaultblocks_weeks = 'participants,activity_modules,search_forums,course_list:news_items,calendar_upcoming,recent_activity';