I am working with PowerShell to create a renaming script for a number of files in a directory.
Two questions here:
I have a string variable $strPrefix = "ACV-100-" and an integer counter $intInc = 000001 and I wish to increment the counter $intInc 1 -> 2 and then concatenate the two and store it in a variable $strCPrefix in the following format: ACV-100-000002.
I believe the $intInc will need to be cast in order to convert it once incrementing is complete but I am unsure how to do this.
Secondly, I have found that the script will display 000001 as 1, 000101 as 101 and so on... I need the full 6 digits to be displayed as this will form a file name. How do I keep or pad the numbers before I process the concatenation?
$intInc = 1
$strCprefix = $strprefix + "{0:000000}" -f $intInc # give ACV-100-000001
hope can help
This should do it:
$intInc = 1
...
$filename = $strPrefix + ('0' * (6 - $intInc.ToSTring().Length)) + ($intInc++).ToString()
Related
I want the variable value to be processed by string interpolation.
val temp = "1 to 10 by 2"
println(s"$temp")
output expected:
inexact Range 1 to 10 by 2
but getting
1 to 10 by 2
is there any way to get this way done?
EDIT
The normal case for using StringContext is:
$> s"${1 to 10 by 2}"
inexact Range 1 to 10 by 2
This return the Range from 1 to 10 with the step value of 2.
And String context won't work on variable, so can there be a way I can do like
$> val temp = "1 to 10 by 2"
$> s"${$temp}" //hypothetical
such that the interpreter will evaluate this as
s"${$temp}" => s"${1 to 10 by 2}" => Range from 1 to 10 by step of 2 = {1,3,5,7,9}
By setting a string value to temp you are doing just that - creating a flat String. If you want this to be actual code, then you need to drop the quotes:
val temp = 1 to 10 by 2
Then you can print the results:
println(s"$temp")
This will print the following output string:
inexact Range 1 to 10 by 2
This is the toString(...) output of a variable representing a Range. If you want to print the actual results of the 1 to 10 by 2 computation, you need to do something like this:
val resultsAsString = temp.mkString(",")
println(resultsAsString)
> 1,3,5,7,9
or even this (watch out: here the curly brackets { } are used not for string interpolation but simply as normal string characters):
println(s"{$resultsAsString}")
> {1,3,5,7,9}
Edit
If what you want is to actually interpret/compile Scala code on the fly (not recommended though - for security reasons, among others), then you may be interested in this:
https://ammonite.io/ - Ammonite, Scala scripting
In any case, to interpret your code from a String, you may try using this:
https://docs.scala-lang.org/overviews/repl/embedding.html
See these lines:
val scripter = new ScriptEngineManager().getEngineByName("scala")
scripter.eval("""println("hello, world")""")
Most of our files are named in a uniform syntax:
B0????????.????.??????.jpg
However occassionaly we see:
?????.B0????????.????.jpg
or
?????????.B0????????.jpg
I need a PowerShell script to pull the 'B0' and the next 8 digits. Traditionally we have just trimmed the string when the file names are uniform, but that is failing with these variations.
Does anyone have a bit of PS logic that can pull 'B0' and the next 8 digits from a string/filename?
Thanks!
$file = "?????.B0????????.????.jpg"
$index = ($file).IndexOf("B0")
$yourCharacters = ($file).Substring($index, 10)
$file.Replace($yourCharacters, "")
I have one file .mat for each condition (4) and each subject (24). So, I have in total 96 files .mat.
Example:
cond1_sbj5_ToProcess_av.mat, cond1_sbj7_ToProcess_av.mat, cond1_sbj10_ToProcess_av.mat, etc.
cond2_sbj5_ToProcess_av.mat, cond2_sbj7_ToProcess_av.mat, cond2_sbj10_ToProcess_av.mat, etc.
cond3_sbj5_ToProcess_av.mat, cond3_sbj7_ToProcess_av.mat, cond3_sbj10_ToProcess_av.mat, etc.
cond4_sbj5_ToProcess_av.mat, cond4_sbj7_ToProcess_av.mat, cond4_sbj10_ToProcess_av.mat, etc.
In each file, depending on the condition, I have a variable that is 66x3000single. AA1 for condition1, AA2 for condition2, AA3 for condition3, AA4 for condition4.
I would like to concatenate on the third dimension AA1,AA2,AA3,AA4 for each subject, in a loop.
So, for each subject I should obtain a 3D matrix/structure with 4 'sheets' as third dimension.
Any suggestion? Thanks a lot.
ok new try with all previous approaches deleted. Here you go. The problem was that cond_number was a string and using a string as an index created the 52, also the load (filename) was missing. However, I tried it with some dummy files and now it works, but only if your subject numbers are running from 1 to 24 and not like 1 5 7 9 19 .... If that is the case you need to modify the code accordingly. Good luck!
clear all
close all
origindir = 'c:\yourdirectory';
cd (origindir)
av_files = dir(fullfile('*.mat'));
mymatrix = zeros(24,66,3000,4);
for ifile = 1:size(av_files,1)
filename = av_files(ifile).name;
load(filename)
if ~isempty(str2num(filename(10:11)))
sub_number = str2num(filename(10:11));
else
sub_number = str2num(filename(10));
end
cond_number_str = filename(5);
cond_number = str2double(cond_number_str);
varname = strcat('AA',cond_number_str);
mymatrix(sub_number,:,:,cond_number)=eval(sprintf(varname));
end
for sub = 1:24
varname2 = strcat('newmat',num2str(sub));
eval([sprintf('%s = squeeze(mymatrix(%i,:,:,:));',varname2,sub)])
end
I have a folder full of xls files, named data_00001 through data_10000. Each file has a dozen or so identically named tabs full of RV's. I am interested in reading all files and tabs and creating histograms of the RV's.
Is there a way to read in the last 5 digits of the file names and attached them to each tab name (which I saved as a variable)?
I used regexp to extract the number as a string and converted it to a double, and I used a for loop to save variable X{1,k}. How can I incorporate the saved double into this variable?
Are you looking for something like this?
filenames = ['data_00001','data_10000'];
nums = regexp(filenames, '[0-9]+', 'match');
tag = 'TAG';
for i=1:size(nums,2)
eval(['A_' tag '_' sprintf("%s",nums{1,i}) ' = zeros(1)']);
end
It creates matrices (zero in this case) with variable names
A_TAG_00001 = 0
A_TAG_10000 = 0
This question already has answers here:
Fastest way to import CSV files in MATLAB
(4 answers)
Closed 9 years ago.
I'm trying to read a .txt file that is ';' delimited with date in the 'header' and diferent columns after the 'header'. I'm using quotes to HEADER because it's more like a parameter line.
So, the .txt is like (the other lines have the same number of columns):
15/07/2013;66;157
DDD;3;1;0;1;1;1;-0.565
DDD;8;2;0;2;1;1;-0.345
DDD;9;3;2;3;1;2;-0.643
DDD;8;1;3;5;1;3;-0.025
DDD;8;1;0;9;1;4;-0.411
DDD;15;1;5;4;1;5;-0.09
DDD;12;1;0;5;1;6;-0.445
DDD;13;1;0;7;1;7;-0.064
I want to read and create a matrix, that contains each data in one cell, like:
matrix =
[15/07/2013 66 157
DDD 3 1 0 1 1 1 -0,565
DDD 8 2 0 2 1 1 -0,345
DDD 9 3 2 3 1 2 -0,643
...]
I've tried textscan, cvsread, textread and nothing works!
Thanks in advance!
Edit: Actually, I found a WAY FASTER code to do this!
From my past experience, MATLAB does not like strings and numbers to be in the same matrix, so you would be forced to use a cell.
You can do this relatively easily with some simple paring.
fid = fopen('temp.txt','r'); %# open file for reading
count = 1;
content = {};
while ~feof(fid)
line = strtrim(fgets(fid)); %# read line by line
parts = regexp(line,';','split');
for i = 1:numel(parts)
temp = regexp(parts{i},'-?[0-9]*\.?[0-9]*(i|j)?','match');
if numel(temp) >= 1 && strcmpi(temp{1},parts{i})
parts{i} = str2double(parts{i}) ;
end
end
content{count} = parts;
count = count + 1;
end
fclose(fid);
numRows = size(content,2)-1;
whole = cell(numRows,8);
for i = 1:numRows
for j = 1:8
whole{i,j} = content{i+1}{j};
end
end
content = {content{1},whole};
UPDATE
I added some stuff to put everything into a single cell array, all of the data outside of the header. I do not know if you wand the header to also be in that 8 column array, but if you do here is some code to do that
numRows = size(content,2);
whole = cell(numRows,8);
for i = 1:numRows
for j = 1:min([size(content{i},2),8])
whole{i,j} = content{i}{j};
end
end
whole