Is it possible to wrap an existing function with another function using the same name in fish? - fish

I'd like to create a cd function which expands on the current cd function which (on my machine) lives at /usr/local/Cellar/fish/2.6.0/share/fish/functions/cd.fish. Is this possible?
So something like this:
function cd
if i_can_handle_it
do_my_thing_with $argv
else
call /usr/local/Cellar/fish/2.6.0/share/fish/functions/cd.fish $argv
end
end

You can't have two functions with the same name. You can have a builtin, function, and external command with the same name. The standard solution to this problem is to first rename the function you want to wrap: functions -c cd cd2. Then define your cd function that calls cd2. Note that this works regardless of whether cd has already been run and thus defined because changing the name first causes it to be autoloaded.

Related

Matlab: set current working directory as top priority in path

In one of my projects, I have a Matlab function called eom.m. When I try to call it, I get errors. I have realised this is because Matlab calls a simulink file, eom.slx, instead, which is in one of the toolboxes.
I would prefer not to rename the function, so I was wondering how I could change the order in the Matlab path so that the folder I call Matlab from always has top priority. That is to say how I can ensure that the files in my current working directory are always those that are called in fact.
Thank you for the help!
You can do it programmatically using addpath with the '-begin' option.
You can use command syntax:
addpath c:/path/you/want -begin
Enclose with quotes if the path contains spaces:
addpath 'c:/path /you/ want' -begin
Alternatively, you can use function syntax:
addpath('c:/path/you/want', '-begin')
This allows having the path stored in a variable:
p = 'c:/path/you/want';
addpath(p, '-begin')

undefined variable or function

I have created a function in MATLAB & have saved it as an m file. When I run my function, it's fine. However using the Windows 7 scheduler it goes to run my function and gives the error message 'Undefined variable 'myMethod' or function 'myMethod.m'.
When I run the which('myMethod.m') it returns the correct folder so not sure what this error message is about?
The pwd method returns the correct address of where my function is too, C:\SomeFolder\MATLAB\Me
Probably its simply not finding the function because it is not on the path.
Assuming you can run builtin functions via the scheduler, try something like this:
p = path
save p
% save c:\ p
In case you cannot even find the saved file, use the last line instead.
Match the path with your files location and presumably the path does not contain the folder which holds your file.

Sending commands to FWTools via Matlab?

I am trying to use Matlab to send commands to FWTools in order to project a bunch of .shp files.
I am able to start FWTools from Matlab but I cant figure out how to send it commands without my interaction.
So far FWTools starts and my matlab command window and acts like the FWTools shell. I type commands and it will run, but I want to be able to send them in my .m file. Once FWTools starts up, my script will not continue to the next line of code.
My code so far:
cmd = 'ogr2ogr -s_srs "EPSG:4326" -t_srs "EPSG:3006" out_sweref99tm.shp in_wgs84.shp';
system('C:\Windows\system32\cmd.exe \K "d:\FWTools2.4.7\setfw.bat" ')
sprintf('%s',cmd) % try to send cmd to matlab command window aka FWTools
I also tried to put the cmd into my system call but that returned an error since it appears that I am trying to call FWTools with a super long string afterwards :(
cmd = 'ogr2ogr -s_srs "EPSG:4326" -t_srs "EPSG:3006" out_sweref99tm.shp in_wgs84.shp';
system(['C:\Windows\system32\cmd.exe \K "d:\FWTools2.4.7\setfw.bat" ',cmd])
The reason I am using FWTools is that I am projecting from WGS84 to SWEREF99TM and (from my understanding) its not easy to get the final projection via Matlab's mapping toolbox. Please correct me if I am wrong as I would love to keep it to Matlab.
And if possible, I would like to stay in Matlab and not move to Python.
Thanks.
Norris
The call of C:\Windows\system32\cmd.exe is not necessary.
Try this:
cmd = 'ogr2ogr -s_srs "EPSG:4326" -t_srs "EPSG:3006" out_sweref99tm.shp in_wgs84.shp';
system(['d: && cd d:\FWTools2.4.7\ && setfw.bat ',cmd])
or if you actually need to pass your parameters after the first call and not with:
system(['d: && cd d:\FWTools2.4.7\ && setfw.bat && ',cmd])
Maybe you can post a screenshot of how it looks like when you do it manually.
requested explanation:
The && operator is implicating that the following command is executed "in the next line". It's like you press enter in the cmd window and then you have to type the next command.
For example cd d:\FWTools2.4.7\ does never work directly, as the default is usually "c:". So firstly have to type d: and in the next step you change to your folder cd d:\FWTools2.4.7\

How do I dynamically select a completion function in zsh?

Say I have a script foo which can be called as:
foo git clone http://example.com
or as:
foo print 12
etc.
How can I make a compdef that delegates to $argv[0] here to allow it to handle completion?
You can redispatch into the normal completion system by calling the _normal function, but first you need to modify some of the state so that it will ignore your program name and possibly its arguments. A very simple version of this can be done with:
#compdef foo
shift words
(( CURRENT-- ))
_normal
If you need to get more complicated that that (which is likely), you can take a look at the completion definitions for other commands that call _normal, such as the completions for env, sudo, or fakeroot.

Executing a Powershell function (which takes parameters) from Powershell command shell

I have a powershell script which is a function and takes parameters. From with the powershell command shell, how do I execute a function? It seems like it works differently for different users.
Is your script, simply a script or does it contain a function? If it is a script and takes parameters it will look something like this:
-- top of file foo.ps1 --
param($param1, $param2)
<script here>
You invoke that just like a cmdlet excecpt that if you are running from the current dir you have to specify the path to the script like so:
.\foo.ps1 a b
Also note that you pass arguments to scripts (and functions) space separated just like you do with cmdlets.
You mentioned function, so if you script looks like this you have a couple of options:
-- top of file foo.ps1 --
function foo ($param1, $param2) {
<script here>
}
If you run foo.ps1 like above, nothing will happen other than you will define a function called foo in a temporary scope and that scope will go away when the script exits. You could add a line to the bottom of the script that actually calls the foo function. But perhaps you are intending on using this script more as a reusable function library. In that case you probably want to load the functions into the current scope. You can do that with the dot source operator . like so:
C:\PS> . .\foo.ps1
C:\PS> foo a b
Now function foo will be defined at the global level. Note that you could do the same thing within another script which will load that function into the script's scope.
Take a look at this post, maybe it is the right for you.
How to pass command-line arguments to a PowerShell ps1 file
Anyway, the built-in $args variable is an array that holds all the command line arguments.