Moving file E93: More than one match - function

The following function works (it opens and moves the desired file to a new or preexisting directory). But, I receive an error message concerning line 11 of the function execute 'bwipeout '.expand(s:oldFileName). The error I receive is E93: More than one match for <the old file name>. I don't understand how this is possible because it has already successfully moved the file to the s:newFileName and the old file has been deleted. The new file's buffer doesn't have the same name as the old buffer. So, how would there be more than one match for the name of the old buffer?
command! -nargs=0 -bar MoveFile call s:functionMoveFile()
function s:functionMoveFile() abort
call dirvish#open("edit", 0)
let s:oldFileName = expand("%:p")
call inputsave()
let s:newFileName = input("Move file here: ",expand(s:oldFileName),"file")
call inputrestore()
if s:newFileName != '' && s:newFileName != s:oldFileName
execute 'sav '.fnameescape(s:newFileName)
let s:newFileDirectory = expand("%:p:h")
call delete(s:oldFileName)
execute 'Dirvish '.expand(s:newFileDirectory)
execute 'bwipeout '.expand(s:oldFileName)
endif
endfunction
I can prepend silent! to line 11 to avoid the error message. But, that isn't a proper fix. I don't know what else I should try because I am not asking vim to wipeout the new buffer and the old file's name doesn't match the new one. The command works as desired, but I would like to know what is incorrect about what I'm doing.
Thank you for your help.

Related

Rerun the program after it ends automatically in octave

Using OCTAVE only...
How can I rerun the code automatically after it ends. Like I want to make a program in which if the input is incorrect value it will end the program and rerun again.
I tried that by writting there a file name and it works but this will only work until I change my file name.
You can wrap your main script in a wrapper script which performs the loop.
% In main.m
disp( 'Hello from main' );
Question = "Do you want to rerun? ";
Response = input( Question, 's');
% in wrapper.m
Response = 'yes';
while strcmp( Response, 'yes' )
main
end

Why Octave doesn't see this object?

I wanted to code a function in octave, however I found some problems during doing so:
N=700;
T=900;
lambda=N/T;
x=randn(N,T);
s=std(x(:));
r=x*x'/T;
l=eig(r);
lambda_plus=(s^2)*(1+sqrt(lambda))^2;
lambda_minus=(s^2)*(1-sqrt(lambda))^2;
# Define a function - value of this function depenends
# on position of x
function kiki = avg (x)
if (x <= lambda_plus && x >= lambda_minus)
(1/(2*pi*lambda*x*s^(2)))*sqrt((lambda_plus-x)*(x-lambda_minus));
else
0
endif
endfunction
Then I wanted to run this function so I did run avg(2) but it didn't work.
Error I saw was:
error: 'lambda_plus' undefined near line 15, column 15
but it's not true! lambda_plus is defined before the definition of the function!
I read that this problem might be, because octave doesn't see our function and the solution is to:
(1) Save the file with name of the function - in my case avg.m
(2) Open a new file and in new file run your function
I did exactly what they were saying and in newly created file I ran avg(2) but unfortunately with exactly same result.

Error : 'x' undefined

I got a problem with running Octave function (ODE), I've tried already present solutions for this problem but nothing is working. I've also tried by saving my filename as egzamin.m but it too not worked.
Code from octave :
function dx=egzamin(x,t)
dx=zeros(4,1);
b=0;
g=9.81;
x1=x(1);
y1=x(2);
Vx=x(3);
Vy=x(4);
dx(1)=Vx;
dx(2)=Vy;
dx(3)=-b*Vx*sqrt(Vx.^2+Vy.^2);
dx(4)=-b*Vy*sqrt(Vx.^2+Vy.^2)-g;
endfunction
N=mod(291813,100);
x1=0;
y1=0;
Vx=20+N;
Vy=20+N;
t=0:0.01:500;
sol=lsode("egzamin",[x1,y1,Vx,Vy],t);
plot(sol(:,1),sol(:,2))
The error is :
error: 'x' undefined near line 5 column 4
error: called from
egzamin at line 5 column 3
Since the file starts with function, it is not a script file,
as explained in the doc:
Unlike a function file, a script file must not begin with the keyword
function
Add any statement (even dummy like 1;) before the function line to get a script file.
# dummy statement to get a script file instead of a function file
1;
function dx=egzamin(x,t)
g = 9.81;
Vx = x(3);
Vy = x(4);
dx = [Vx, Vy, 0, -g];
endfunction
N=mod(291813,100);
x1=0;
y1=0;
Vx=20+N;
Vy=20+N;
t=0:0.01:500;
sol=lsode("egzamin",[x1,y1,Vx,Vy],t);
plot(sol(:,1),sol(:,2))
A very clear explanation of what's going on is given here.
You need to save the function (thus from function to endfunction and naught else) as egzamin.m, and then execute the rest of the code in a script or at the command line. Alternatively, provided Octave does that the same as what MATLAB does nowadays, first put your script (N=(..) to plot()) and then the function.
This is necessary since you are defining your function first, so it doesn't have any inputs yet, as you don't define them until later. The function needs to have its inputs defined before it executes, hence you need to save your function separately.
You can of course save your "script" bit, thus everything which is currently below your function declaration, as a function as well, simply don't give it in- and outputs, or, set all the input parameters here as well. (Which I wouldn't do as it's the same as your
egzamin then.) e.g.
function []=MyFunc()
N=mod(291813,100);
x1=0;
y1=0;
Vx=20+N;
Vy=20+N;
t=0:0.01:500;
sol=lsode("egzamin",[x1,y1,Vx,Vy],t);
plot(sol(:,1),sol(:,2))
endfunction

Unable to delete file through function in Scilab

I have written the following code to plot a graph with the data present in the 'datafile'. After the graph has been plotted, I want to delete the file.
function plot_torque(datafile)
//This will call a datafile and plot the graph of net_torque vs. time
verbose = 1;
// Columns to plot
x_col = 1;
y_col = 2;
// open the datafile
file1 = file('open', datafile,'old');
data1 = read(file1, -1, 4);
time = data1(:,x_col);
torque = data1(:,y_col);
plot(time, torque, '.-b');
xtitle("Torque Generated vs. Time" ,"Time(s)" , "Torque Generated(Nm/m)");
file('close',file());
//%________________%
endfunction
In the place that I have marked as //%________% I have tried
deletefile(datafile);
and
mdelete(datafile);
None of them have worked.
And I have set the working directory to where the above '.sci' file is present and the 'datafile' is present. I am using scilab-5.4.1.
You probably leave (left) the file open. Try this:
fil="d:\Attila\PROJECTS\Scilab\Stackoverflow\file_to_delete.txt"; //change it!
fprintfMat(fil,rand(3,3),"%.2g"); //fill with some data
fd=mopen(fil,"r"); //open
//do something with the file
mclose(fd); //close
//if you neglect (comment out) this previous line, the file remains open,
//and scilab can not delete it!
//If you made this "mistake", first you should close it by executing:
// mclose("all");
//otherwise the file remains open until you close (and restart) Scilab!
mdelete(fil); //this works for me

vim key sequence as function argument

I want to execute the command "yiw:s/\<<C-r>"\>/<C-r>"/g<Left><Left>" by key sequence.
So I make a mapping
nnoremap <F7> yiw:s/\<<C-r>"\>/<C-r>"/g<Left><Left>
This mapping copy the word under cursor, then the string :s/\<">/"/g" (where " are substituted by the copied word) appears in the command line and the cursor in the command line is at the end of replacement statement.
I also want to save cursor position before this command and restore after.
function! SafeCommand(cmd)
let line = line('.')
let col = col('.')
// execute cmd here
call cursor( line, col )
endfunction
How to do that?
Normally, you'd just put the entire (complex) command in a function, and invoke that function from the :nnoremap. But that doesn't work for incomplete commands, like the template :substitute that your mapping represents. For that, you need to include the save / restore parts into the command-line (though that's ugly):
:fun! Save()
let s:line = line('.')
let s:col = col('.')
:endfun
:fun! Restore()
call cursor( s:line, s:col )
:endfun
:nnoremap <F7> yiw:call Save()<Bar>s/\<<C-r>"\>/<C-r>"/g<Bar>call Restore()<Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left><Left>