Rename file from xx_02.csv to xx.csv [closed] - matlab

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a folder 'a' with about 200 files with names xx_out_02.csv and I want to rename them to xx_out.csv. May be using Matlab or running some script. I tried it in cmd but I have to run the command for each and every file.
Can someone help me here?
Best Regards
Dilip

You can use the movefilefunction from matlab.
Here is an example:
clc
addpath('yourdir')
csvf = dir('yourdir/*.csv');
numberOfcsv = numel(csvf);
for ii = 1:numberOfcsv
file = csvf(ii).name;
movefile(sprintf('yourdir/%s', file), sprintf('yourdir/x%03d_out.csv', ii), 'f');
end

Your question is unclear. I'm assuming
You want to strip off substrings of the form _ followed by one or more digits right before .csv.
The resulting target names are all different. For example, you have files such as xx_out_02.csv and yy_out_01.csv, but not xx_out_02.csv and xx_out_01.csv.
Operating system? I'm considering Windows. For other systems you can change the system line below with the appropriate system comand. Or better use movefile as in SamuelNLP's answer.
Code:
files = dir('*.csv');
names = {files.name};
for n = 1:numel(names)
name = names{n};
name_new = regexprep(name, '_\d+(?=\.csv$)', '');
system(['ren ' name ' ' name_new]); %// MS-DOS command to rename file
end

Related

How to read wave files one by one by sprintf command? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
enter image description hereThe wave file are like:The first digit- speaker id, second digit-utterence id
00
01
02
03
04
.
.
.
59
I tried using
for m=0:5
for y=0:9
file=sprintf('%s%d%d','E:\0 (1)\0\',m,y);
[s,fs]=wavread(file);
end
end
But this gives error. Alternatively I tried
mypath = 'E:\\0 (1)\\0\\';
filename = sprintf([mypath '%s%d.wav'],m,y);
[s,fs] = audioread(filename);
But it gave error as :
Function is not defined for sparse inputs.
Please help me.
It seems that your problem comes from string concatenation. But since neither your file format nor your path format are clear, this is just a sample code that performs something close to what you are attempting to do:
for m = 0:5
m_curr = num2str(m);
for y = 0:9
file = ['E:\' m_curr num2str(y) '.wav'];
[s,fs] = wavread(file);
end
end
Set a debug breakpoint on the wavread line and check if the format matches your needs. If not, change it accordingly.

Awk command to powershell [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Hi i try to transcript an awk command to powershell
i have a text file
SQLBefore:=
UPDATE SYSADM.PS_S1_R_FLWUP
SET S1_FLWUP_DATE_CHK=SYSTIMESTAMP
, S1_FLWUP_DATE_LAST=NULL
WHERE S1_FLWUP_NAME='GGGGG_ETAT_JJ'
SQLAfter:=
UPDATE SYSADM.PS_S1_R_FLWUP
SET S1_FLWUP_DATE_LAST=SYSTIMESTAMP
, S1_FLWUP_STAT_SID=1
WHERE S1_FLWUP_NAME='TTTTT_ETAT_'
SQLFailed:=
UPDATE SYSADM.PS_S1_R_FLWUP
SET S1_FLWUP_DATE_LAST=S1_FLWUP_DATE_FRST
, S1_FLWUP_STAT_SID=3
WHERE S1_FLWUP_NAME='JJJJ_ETAT_JJJ'
And i would like to do the same than this unix command in powershell
cat $this_file|awk '/SQLAfter/,/SQLFailed/ {print $0}'| grep -v SQL|sed -e 's/^$//'
It's return
UPDATE SYSADM.PS_S1_R_FLWUP
SET S1_FLWUP_DATE_CHK=SYSTIMESTAMP
, S1_FLWUP_DATE_LAST=NULL
WHERE S1_FLWUP_NAME='GGGGG_ETAT_JJ'
I'm getting stuck to do the delimiter like awk, between "SQLBefore:=" and "SQLAfter:=" with powershell
Thanks for your help.
I'm a beginner of powershell sorry for my english
One possibility, assuming I'm reading the question correctly (however "bad" you think your English might be I assure you it's considerably better than my AWK).
((get-content testfile.txt -raw) -split 'SQL(?:Before|After):=')[1]

How to check output from command - PERL [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
i have a problem, I am new in Perl, so.
I want to get output from command i send to check if its done, for example:
In my module i run command my_zip_command file a_lot_of_file' and i want to my module wait until the massage 'all file were zip correctly' will be printed.
I tried get the STDOUT but it didnt work for me, or i just doing it wrong.
bash>my_zip_command file a_lot_of_file
>ziping file1 100%
>ziping file2 100%
>ziping file3 100%
>all file were zip correctly
bash>
Thanks for all your help
If you are trying to
Run a command from your perl script and
Capture the output of that command inside your perl script
this is the simplest way I can think of.
my #output = `my_zip_command file a_lot_of_file`;
#output will hold the complete output of the command.

Extract text, Matlab [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to find a way to extract text in a specific and efficient way
as in this example:
'Hello Mr. Jack Andrew , your number is 894Gfsf , and your Bank ID # 734234"
I want a way to get the Name, the Number and the Bank ID Number.
I want to write software that deals with different text files and get those required values. I may not know the exact order but it must be a template like a bank statement or something.
Thanks!
It's a bit hard to understand what exactly is the problem.. If all you need to do is to split strings, here's a possible way to do it:
str = 'Hello Mr. Jack Andrew , your number is 894Gfsf , and your Bank ID # 734234';
tokenized = strsplit(str,' ');
Name = strjoin([tokenized(3:4)],' ');
Number = tokenized{9};
Account = tokenized{end};
Alternatively, for splitting you could use regexp(...,'split') or regexp(...,'tokens');
I think you want regular expressions for this. Here's an example:
str = 'Hello Mr. Jack Andrew , your number is 894Gfsf , and your Bank ID # 734234';
matches=regexp(str, 'your number is (\w+).*Bank ID # (\d+)', 'tokens');
matches{1}
ans =
'894Gfsf' '734234'
My suggestion would be to make a whole array of strings with sample patterns that you want to match, then build a set of regular expressions that collectively match all of your samples. Try each regexp in sequence until you find one that matches.
To do this, you will need to learn about regular expressions.

Opening multiple files with similar names in MATLAB: importdata cannot open the files given the name by strcat or dir [duplicate]

This question already has answers here:
How can I load 100 files with similar names and/or string in just one step in MATLAB?
(3 answers)
Closed 9 years ago.
As Shai and CTZStef suggested, when I have to open multiple files with similar names in MATLAB, I have to do
for k=1:size(fls)
fileName = strcat('int_',num2str(k),'.ASC');
A(k) = importdata(fileName, ' ', 9);
x(k) = A(k).data(:,1);
y(k) = A(k).data(:,2);
end
or also
fls = dir('int_00_*.ASC');
for fi=1:numel(fls)
A(fi) = importdata(fls(fi).name, ' ',9);
end
Well, the problem is that none of them works.
What should I do? Is it a problem with my MATLAB version?
You need to follow the answers you got more closely:
The strcat solution CANNOT handle the zero padding of the file names. You'll ahve to manually rename all files from 'int_001.ASC' to 'int_1.ASC'.
UPDATE (due to #DedekMraz's comment): you'll need to modify the strcat input string to strcat('int_', num2str( k, '%03d' ), '.ASC');
You can use a strategy similar to this one. See the update to the answer you already got.
The input you gave your dir function is wrong it has to be dir('int_*.ASC') and not dir('int_00_*.ASC').