this question about matlab:
i'm running a loop and each iteration a new set of data is produced, and I want it to be saved in a new file each time. I also overwrite old files by changing the name. Looks like this:
name_each_iter = strrep(some_source,'.string.mat','string_new.(j).mat')
and what I#m struggling here is the iteration so that I obtain files:
...string_new.1.mat
...string_new.2.mat
etc.
I was trying with various combination of () [] {} as well as 'string_new.'j'.mat' (which gave syntax error)
How can it be done?
Strings are just vectors of characters. So if you want to iteratively create filenames here's an example of how you would do it:
for j = 1:10,
filename = ['string_new.' num2str(j) '.mat'];
disp(filename)
end
The above code will create the following output:
string_new.1.mat
string_new.2.mat
string_new.3.mat
string_new.4.mat
string_new.5.mat
string_new.6.mat
string_new.7.mat
string_new.8.mat
string_new.9.mat
string_new.10.mat
You could also generate all file names in advance using NUM2STR:
>> filenames = cellstr(num2str((1:10)','string_new.%02d.mat'))
filenames =
'string_new.01.mat'
'string_new.02.mat'
'string_new.03.mat'
'string_new.04.mat'
'string_new.05.mat'
'string_new.06.mat'
'string_new.07.mat'
'string_new.08.mat'
'string_new.09.mat'
'string_new.10.mat'
Now access the cell array contents as filenames{i} in each iteration
sprintf is very useful for this:
for ii=5:12
filename = sprintf('data_%02d.mat',ii)
end
this assigns the following strings to filename:
data_05.mat
data_06.mat
data_07.mat
data_08.mat
data_09.mat
data_10.mat
data_11.mat
data_12.mat
notice the zero padding. sprintf in general is useful if you want parameterized formatted strings.
For creating a name based of an already existing file, you can use regexp to detect the '_new.(number).mat' and change the string depending on what regexp finds:
original_filename = 'data.string.mat';
im = regexp(original_filename,'_new.\d+.mat')
if isempty(im) % original file, no _new.(j) detected
newname = [original_filename(1:end-4) '_new.1.mat'];
else
num = str2double(original_filename(im(end)+5:end-4));
newname = sprintf('%s_new.%d.mat',original_filename(1:im(end)-1),num+1);
end
This does exactly that, and produces:
data.string_new.1.mat
data.string_new.2.mat
data.string_new.3.mat
...
data.string_new.9.mat
data.string_new.10.mat
data.string_new.11.mat
when iterating the above function, starting with 'data.string.mat'
I have a string listed below with apostrophe.
stringVar = '''L''hopital''s rule'''
when I do sprintf i.e. sprintf(stringVar) it prints this 'L'hopital's rule'.
Now, what I would like to do is do an sprintf so that when I print it it will display as
'L''hopital''s rule'
Now I know I can easily do this '''L''''hopital''''s rule''' but would prefer to do it programatically. What's the best/correct way of approaching this problem. Note: I will need to handle many of these e.g. '''L''Environment'''.
ind = regexp(stringVar, '\w''\w') + 1; %// detect quotes between word characters
stringVarRep = stringVar(sort([1:numel(stringVar) ind])); %// repeat those quotes
I have a data stored in below format, no delimeter and digit domain is {0,1}. With using octave, taking the digits and storing them in martix is reaised a problem for me. I have not managed below scnerio. So, How can I take those digits and store them on matrix as told at below?
Data in File, 32 x 32 digits
00000000000000000000000000000000
00000000001111110000000000000000
...
00000010000000100001000000000000
how to store data
matrix[1, 1:32] = 00000000000000000000000000000000
matrix[2, 1:32] = 00000000001111110000000000000000
. . .
matrix[32, 1:32] = 00000010000000100001000000000000
OR
matrix[1, 1:32] = 00000000000000000000000000000000
matrix[1, 33:64] = 00000000001111110000000000000000
. . .
matrix[1, 993:1024] = 00000010000000100001000000000000
One possible solution is to read the data as a string first:
octave> textread('foo.dat', '%s', 'headerlines', 2)
ans =
{
[1,1] = 00000000000000000000000000000000
[2,1] = 00000000001111110000000000000000
...
}
If these are binary representations of decimals, you may find bin2dec() useful.
This would do the trick (though I don't know how well that third input to fread and arrayfun work with Octave, tested this on Matlab):
fid = fopen('a.txt','rt');
str = fread(fid,inf,'char=>char');
st = fclose(fid);
qrn = str==10|str==13;
str(qrn) = [];
yourMat = reshape(arrayfun(#str2num,str),find(qrn,1)-1,[]).'
Assuming you don't have header lines, you can read the text in as a cell arrray of strings like so:
C = textread('names.txt', '%s');
Then, in general for all numbers from 0 to 9, you can transform this into a matrix like so:
M = vertcat(S{:})-'0';
If performance is an issue you can look into other ways to import the strings, but this should get the job done.
I have never used Matlab, but asuming it reads files the same way Octave does, and if using an external tool is OK, you could try replacing the characters to add a delimiter using a text editor. You could change every "0" to "0," and every "1" to "1," and then simply load the file.
(This would add a delimiter at the end of every line. In case that creates a problem, you could try replacing your text by pairs instead "00"->"0,0" "10" -> "1,0" and so on)
In case the file is too big for a normal editor, you might even try replacing the characters with sed:
sed -i 's/charactertoreplace/newcharacter/g' yourfile.txt
I try to read data from file but I have no idea how to use fscanf normally.
Have file like this
surname1 A.A.(16 char everytime) 23323 3232 232322 ....
surname2 A.A. 23322 3232 232322 ....
surname3 A.A. 23322 3232 232322 ....
How to use fscanf here?
I do:
fid=fopen( 'name.txt', 'rb' );
and variations of S = fscanf(fid, '%16c,%s,%s,%s');
What should I write to have working program?
If you are only interested in data and the format of your text file stays always the same as you have shown, you can easily follows these two command line:
fid = importdata('yourfile.txt');
res = fid.data;
Hope this helps.
I have a text file that contains multiple headers.
It looks like this:
Date,time,*10ms,%%,%%,%%,%%,%%,%%,%%,%%,%%,%%,DETAILS.txt;D;%%;10
11/08/19,13:19:28,03,446,0,545.75,0,6,0,0,0,14,0
11/08/19,13:19:29,05,446,0,549.25,1.9,6,102,1,0,0,0
11/08/19,13:19:30,07,446,0,549.5,1.9,6,102,1,0,0,0
11/08/19,13:19:31,09,446,0,548.75,1.9,6,102,1,0,0,0
.
.
.
.
Date,time,*10ms,%%,%%,%%,%%,%%,%%,%%,%%,%%,%%,DETAILS.txt;D;%%;10
11/08/19,13:19:28,03,446,0,545.75,0,6,0,0,0,14,0
11/08/19,13:19:29,05,446,0,549.25,1.9,6,102,1,0,0,0
11/08/19,13:19:30,07,446,0,549.5,1.9,6,102,1,0,0,0
11/08/19,13:19:31,09,446,0,548.75,1.9,6,102,1,0,0,0
.
.
and so on...
I would like to write a code that deletes these header-rows and stores the rest of the data to a new file.
Could anyone help with this?
Kind regards,
Tamara
readID = fopen('headers.txt', 'r');
writeID = fopen('no_headers.txt', 'w');
while feof(readID) == 0
currLine = fgetl(readID);
if isempty( strfind(currLine, 'Date') )
fprintf(writeID, '%s\n', currLine);
end
end
fclose(readID);
fclose(writeID);
It looks like all the headers are the same, I assume you mean: Date,time,*10ms,%%,%%,%%,%%,%%,%%,%%,%%,%%,%%,DETAILS.txt;D;%%;10
And you wish to use MATLAB, if that's the case, you have to open the file, textscan for that one line and then fgetl to remove it.
No need for regex or anything like that when the line is always the same.