I am writing a code in octave which is the same as well in matlab, with a recursive function. I have a lot of folders and subfolders and inside one of the subfolders, there is a folder which is called img and inside the img folder there are two image files which I read and calculate some data.
Here is the code:
function findin(folder)
foldername = 'img';
filetofind = '*.ppm';
scalefront = 0.05;
scaletop = 0.05;
dirinfo = dir(folder);
dirinfo(~[dirinfo.isdir]) = []; %remove non-directories
tf = ismember( {dirinfo.name}, {'.', '..'});
dirinfo(tf) = []; %remove current and parent directory.
for i=1:length(dirinfo)
if strcmp(dirinfo(i).name, foldername)
files = dir(fullfile(dirinf(i).name, filetofind));
frontimage = strcat(files(1).name);
topimage = strcat(files(2).name);
Angle = getAngle(frontimage, scalefront, topimage, scaletop);
printf('\nAngle for images %s and %s is: &f\n', frontimage, topimage, Angle);
else
findin(dirinfo(i).name);
end
end
printf('done!');
endfunction
The problem is that the if statement never becomes true and in whatever folder I am, it just prints 'done!' according to the number of folders. This means that it doesn't do the recursive call.
Am i doing something wrong with the recursive call??
Related
I want to add value to matrix.
But, I could not do it.
I made the stepfunction for reinforcement learning and I calculated value which named R.
I want to gather R of value and add to matrix named RR.
However, all the calculated values are not added.
Is this because it's done inside a function? If it's not in a function, it works.
Please someone tell me the way to fix it.
% Define the step function
function [NextObservation, Reward, IsDone, LoggedSignals] =
myStepfunction(Action,LoggedSignals,SimplePendulum)
% get the pre statement
statePre = [-pi/2;0];
statePre(1) = SimplePendulum.Theta;
statePre(2) = SimplePendulum.AngularVelocity;
IsDone = false;
% updating states
SimplePendulum.pstep(Action);
% get the statement after updating
state = [-pi/2;0];
state(1) = SimplePendulum.Theta;
state(2) = SimplePendulum.AngularVelocity;
RR = [];
Ball_Target = 10;
Ball_Distance = Ballfunction(SimplePendulum);
R = -abs(Ball_Distance -Ball_Target); ← this the calculated value
RR = [RR,R]; ← I want to add R to RR
if (state(2) > 0) || (SimplePendulum.Y_Position < 0)
IsDone = true;
[InitialObservation, LoggedSignal] = myResetFunction(SimplePendulum);
LoggedSignal.State = [-pi/2 ; 0];
InitialObservation = LoggedSignal.State;
state = InitialObservation;
SimplePendulum.Theta =-pi/2;
SimplePendulum.AngularVelocity = 0;
end
LoggedSignals.State = state;
NextObservation = LoggedSignals.State;
Reward = +max(R);
end
If I understand you correctly, you are trying update RR with this function, and then use/see it outside the loop. This means you need to make two edits:
a) You need to pass RR in and out of the function to update it. You will need to change the first line of your code as shown here, but you will also need to change how you call the function:
function [NextObservation, Reward, IsDone, LoggedSignals, RR] =
myStepfunction(Action,LoggedSignals,SimplePendulum, RR)
b) When you do this RR = [] you delete the content of RR. To initialise RR you will need to move this line to the script that calls this function, making sure it is called before this function is ever called.
I have a file which has multiple file in it. Multiple files have excel files. I could put them on a list
excel_files = []
for path, dirs, files in os.walk(bugra_path):
for filename in files:
if filename == 'desktop.ini': continue
excel_files.append(filename)
The output is like that:
['2019-11-kudup.xlsx', '2019-12-kudup.xlsx', '2019-11-kur.xlsx', '2019-12-kur.xlsx', '2019-11-bilateral_buys.xlsx', '2019-12-bilateral_buys.xlsx', '2019-11-bilateral_sells.xlsx', '2019-12-bilateral_sells.xlsx', '2019-11-dam_buys.xlsx', '2019-12-dam_buys.xlsx', '2019-11-dam_sells.xlsx', '2019-12-dam_sells.xlsx', '2019-11-productions.xlsx', '2019-12-productions.xlsx', '2019-11-ptf_smf.xlsx', '2019-12-ptf_smf.xlsx']
However, this not enough for me, I want to split this list into multiple lists based on its element names. I mean want to achieve this:
kudup_list = ['2019-11-kudup.xlsx', '2019-12-kudup.xlsx']
kur_list = ['2019-11-kur.xlsx', '2019-12-kur.xlsx']
bilateral_buys_list = ['2019-11-bilateral_buys.xlsx', '2019-12-bilateral_buys.xlsx']
bilateral_sells_list = ['2019-11-bilateral_sells.xlsx', '2019-12-bilateral_sells.xlsx']
dam_buys_list = ['2019-11-dam_buys.xlsx', '2019-12-dam_buys.xlsx']
dam_sells_list = ['2019-11-dam_sells.xlsx', '2019-12-dam_sells.xlsx']
productions_list = ['2019-11-productions.xlsx', '2019-12-productions.xlsx']
ptf_smf_list = ['2019-11-ptf_smf.xlsx', '2019-12-ptf_smf.xlsx']
How can I get this output?
Suppose I have the following three paths:
let file = path::Path::new("/home/meurer/test/a/01/foo.txt");
let src = path::Path::new("/home/meurer/test/a");
let dst = path::Path::new("/home/meurer/test/b");
Now, I want to copy file into dst, but for that I need to correct the paths, so that I can have new_file with a path that resolves to /home/meurer/test/b/01/foo.txt. In other words, how do I remove src from file and then append the result to dst?
/home/meurer/test/a/01/foo.txt -> /home/meurer/test/b/01/foo.txt
Note that we can't assume that src will always be this similar to dst.
You can use Path::strip_prefix and Path::join:
use std::path::Path;
fn main() {
let file = Path::new("/home/meurer/test/a/01/foo.txt");
let src = Path::new("/home/meurer/test/a");
let dst = Path::new("/home/meurer/test/b");
let relative = file.strip_prefix(src).expect("Not a prefix");
let result = dst.join(relative);
assert_eq!(result, Path::new("/home/meurer/test/b/01/foo.txt"));
}
As usual, you probably don't want to use expect in your production code, it's only for terseness of the answer.
Im trying to send files from a folder in ubuntu(Pictures) to (Downloads) after they have been processed using storescu.For each file processed by storescu I want it to delete that file.
So far I can do the storescu and then delete the file but I fear if more files come into that folder while it still processing the files it will just delete the files so I want it to process with storescu and then delete that file and move on to the next. Here is want I currently have:
import os, subprocess
src = "/home/pi/Pictures/"
dst = "/home/pi/Downloads/"
listOfFiles = os.listdir(src)
for f in listOfFiles:
os.system("storescu /home/pi/Pictures/" +f)
listOfFiles = os.listdir(src)
for f in listOfFiles:
fullPath = src + "/" + f
subprocess.Popen("mv" + " " + fullPath + " " + dst,shell=True)
This is currently working but like I said it removes the files after it sends them all.
I have a need to create an action that will:
1. copy a selected part (selected by hand) of an image in an already opened file
2. paste selection into new file
3. save new file as jpg file, but not with default file name of "untitled.jpg" - instead use a unique name or use a auto-increment suffix
Because the action will be run multiple times on different selections from the same image, saving each selection with a unique name or auto-incremented suffix would save the step of manually supplying the filename each time a different selection is saved.
I can create an action that gets to the save-as step, but don't know if it is possible to modify the default save as name as described above. Is it possible?
No. Tried it before with no success. You have to save manually.
Don't think this is possible with an action but you can write a script do to it.
I have created a script for similar work. It uses a technique to generate unique filenames and save the file.
/************************************************************************
* Author: Nishant Kumar
* Description: This script iterates through a template to create
* jpg images with id card numbers.
* Date: 08-03-2015
***********************************************************************/
//current id count
var id_count = 0;
//total no of id cards to produce
var total_id_cards = 42;
//no. of cards per sheet
var card_per_sheet = 21;
//Save path related to current file
var save_path = app.activeDocument.path;
//do an iteration, number the cards and save file
do{
//iterate 24 nos in each document
for(var i = 0; i<card_per_sheet; i++){
id_count++;
app.activeDocument.layers[i].textItem.contents = id_count;
}
//Create a jpg document with standard options
jpgSaveOptions = new JPEGSaveOptions();
jpgSaveOptions.embedColorProfile = true;
jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
jpgSaveOptions.matte = MatteType.NONE;
jpgSaveOptions.quality = 12;
//Save jpg with incremental file names (1.jpg, 2.jpg), make sure the path exists
jpgFile = new File( save_path + "/output/" + id_count/card_per_sheet + ".jpeg" );
app.activeDocument.saveAs(jpgFile, jpgSaveOptions, true, Extension.LOWERCASE);
}while(id_count < total_id_cards);
I know this is old, but still. You can use the following script.
How to use a script:
Copy the following script in notepad, and save it in directory similar to "C:\Program Files (x86)\Adobe\Adobe Photoshop CS2\Presets\Scripts" with the extension JSX.
To run the scrip in photoshop, go to File > Scripts > "Your Script".
#target photoshop
main();
function main(){
if(!documents.length) return;
var Name = app.activeDocument.name.replace(/.[^.]+$/, '');
Name = Name.replace(/\d+$/,'');
try{
var savePath = activeDocument.path;
}catch(e){
alert("You must save this document first!");
}
var fileList= savePath.getFiles(Name +"*.jpg").sort().reverse();
var Suffix = 0;
if(fileList.length){
Suffix = Number(fileList[0].name.replace(/\.[^\.]+$/, '').match(/\d+$/));
}
Suffix= zeroPad(Suffix + 1, 4);
var saveFile = File(savePath + "/" + Name + "_" + Suffix + ".jpg");
SaveJPG(saveFile);
}
function SaveJPG(saveFile){
//Create a jpg document with standard options
jpgSaveOptions = new JPEGSaveOptions();
jpgSaveOptions.embedColorProfile = true;
jpgSaveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
jpgSaveOptions.matte = MatteType.NONE;
jpgSaveOptions.quality = 12;
//Save jpg with incremental file names (1.jpg, 2.jpg), make sure the path exists
activeDocument.saveAs(saveFile, jpgSaveOptions, true, Extension.LOWERCASE);
};
function zeroPad(n, s) {
n = n.toString();
while (n.length < s) n = '0' + n;
return n;
};