MATLAB filename separation - matlab

In filename "name" like '10_m1_m2_const_m1_waves_20_90_m2_waves_90_20_20200312_213048' I need to separate
'10_m1_m2_const_m1_waves_20_90_m2_waves_90_20' from '20200312_213048'
name_sep = split(name,"_");
sep = '_';
name_join=[name_sep{1,1} sep name_sep{2,1} sep .....];
is not working, because a number of "_" are variable.
So I need to move a file:
movefile([confpath,name(without 20200312_213048),'.config'],[name(without 20200312_213048), filesep, name, '.config']);
Do you have any idea? Thank you!

Maybe you can try regexp to find the starting position for the separation:
ind = regexp(name,'_\d+_\d+$');
name1 = name(1:ind-1);
name2 = name(ind+1:end);
such that
name1 = 10_m1_m2_const_m1_waves_20_90_m2_waves_90_20
name2 = 20200312_213048
Or the code below with option tokens:
name_sep = cell2mat(regexp(name,'(.*)_(\d+_\d+$)','tokens','match'));
which gives
name_sep =
{
[1,1] = 10_m1_m2_const_m1_waves_20_90_m2_waves_90_20
[1,2] = 20200312_213048
}

You can use strfind. Either if you have a key that is always present before or after the point where you want to split the name:
nm = '10_m1_m2_const_m1_waves_20_90_m2_waves_90_20_20200312_213048';
key = 'waves_90_20_';
idx = strfind(nm,key) + length(key);
nm(idx:end)
Or if you know how may _ are in the part that you want to have:
idx = strfind(nm,'_');
nm(idx(end-2)+1:end)
In both cases, the result is:
'20_20200312_213048'

As long as the timestamp is always at the end of the string, you can use strfind and count backwards from the end of the string:
name = '10_m1_m2_const_m1_waves_20_90_m2_waves_90_20_20200312_213048';
udscr = strfind(name,'_');
name_date = name(udscr(end-1)+1:end)
name_meta = name(1:udscr(end-1)-1)
name_date =
'20200312_213048'
name_meta =
'10_m1_m2_const_m1_waves_20_90_m2_waves_90_20'

Related

Flutter: Replace particular string after '.jpg'

I have strings of jpg name
a = '123a.jpg'
b = '456asa.jpg'
c = '789pa.jpg'
Is there a way to replace the final a before .jpg with b?
new_a = '123b.jpg'
new_b = '456asb.jpg'
new_c = '789pb.jpg'
Using a small work arround:
a = a.split('').reversed.join().replaceFirst('a', 'b').split('').reversed.join();
Try
String newa = a.replace("a.", "b.");
Edit. You can also do
newa = a.replaceAll("a$","b");
Here $ means last index

Extract fields from Structure Array to put into another Structure Array

I have a structure array with a large number of fields that I don't care about, so I want to extract the limited number of fields I DO care about and put it into a separate structure array.
For a structure array of size one, I've done this by creating the new array from scratch, for example:
structOld.a = 1;
structOld.b = 2;
structOld.usefulA = 'useful information';
structOld.usefulB = 'more useful information';
structOld.c = 3;
structOld.d = 'words';
keepFields = {'usefulA','usefulB'};
structNew = struct;
for fn = keepFields
structNew.(fn{:}) = structOld.(fn{:});
end
which gives
structNew =
usefulA: 'useful information'
usefulB: 'more useful information'
Is there a more efficient way of doing this? How can I scale up to an structure array (vector) of size N?
N = 50;
structOld(1).a = 1;
structOld(1).b = 2;
structOld(1).usefulA = 500;
structOld(1).usefulB = 'us';
structOld(1).c = 3;
structOld(1).d = 'ef';
structOld(2).a = 4;
structOld(2).b = 5;
structOld(2).usefulA = 501;
structOld(2).usefulB = 'ul';
structOld(2).c = 6;
structOld(2).d = 'in';
structOld(3).a = 7;
structOld(3).b = '8';
structOld(3).usefulA = 504;
structOld(3).usefulB = 'fo';
structOld(3).c = 9;
structOld(3).d = 'rm';
structOld(N).a = 10;
structOld(N).b = 11;
structOld(N).usefulA = 506;
structOld(N).usefulB = 'at';
structOld(N).c = 12;
structOld(N).d = 'ion';
In this case, I'd like to end up with:
structNew =
1x50 struct array with fields:
usefulA
usefulB
Keeping elements with empty usefulA/usefulB fields is fine; I can get rid of them later if needed.
Using rmfield isn't great because the number of useless fields far outnumbers the useful fields.
You can create a new struct array using existing data as follows:
structNew = struct('usefulA',{structOld.usefulA},'usefulB',{structOld.usefulB});
If you have an arbitrary set of field names that you want to preserve, you could use a loop as follows. Here, I'm first extracting the data from strcutOld into a cell array data, which contains each of the arguments the the struct call in the previous line of code. data{:} is now a comma-separated list of these arguments, the last line of code below is identical to the line above.
keepFields = {'usefulA','usefulB'};
data = cell(2,numel(keepFields));
for ii=1:numel(keepFields)
data{1,ii} = keepFields{ii};
data{2,ii} = {structOld.(keepFields{ii})};
end
structNew = struct(data{:});

Random selection of a member's location in a nested cell of cells: Matlab

I have a nested cell of cells like the one below:
CellArray={1,1,1,{1,1,1,{1,1,{1,{1 1 1 1 1 1 1 1}, 1,1},1,1},1,1,1},1,1,1,{1,1,1,1}};
I need to randomly pick a location in CellArray. All members' locations of CellArray must have same chances to be chosen in the random selection process. Thanks.
You can capture the output of the celldisp function. Then use regex to extrcat indices:
s=evalc('celldisp(CellArray,'''')');
m = regexp(s, '\{[^\=]*\}', 'match');
Thanks to #excaza that suggested a clearer use of regexp
Result:
m =
{
[1,1] = {1}
[1,2] = {2}
[1,3] = {3}
[1,4] = {4}{1}
[1,5] = {4}{2}
[1,6] = {4}{3}
[1,7] = {4}{4}{1}
[1,8] = {4}{4}{2}
[1,9] = {4}{4}{3}{1}
[1,10] = {4}{4}{3}{2}{1}
[1,11] = {4}{4}{3}{2}{2}
[1,12] = {4}{4}{3}{2}{3}
[1,13] = {4}{4}{3}{2}{4}
[1,14] = {4}{4}{3}{2}{5}
[1,15] = {4}{4}{3}{2}{6}
[1,16] = {4}{4}{3}{2}{7}
[1,17] = {4}{4}{3}{2}{8}
[1,18] = {4}{4}{3}{3}
[1,19] = {4}{4}{3}{4}
[1,20] = {4}{4}{4}
[1,21] = {4}{4}{5}
[1,22] = {4}{5}
[1,23] = {4}{6}
[1,24] = {4}{7}
[1,25] = {5}
[1,26] = {6}
[1,27] = {7}
[1,28] = {8}{1}
[1,29] = {8}{2}
[1,30] = {8}{3}
[1,31] = {8}{4}
}
Use randi to select an index:
m{randi(numel(m))}

About save string inside an array

I have question about how can I get from a user multiple inputs(once per line) as a string and save it inside an array ?
I tried something like this :
function[str] = get_data()
st = '';
st{1} = input{'enter the first name','s'};
st{2} = input{'enter the first name','s'};
str = strings(st)
end
Declare your cell beforehand as a cell. Not as a string. Then you can fill it with the inputs.
function[str] = get_data()
st = cell(1,2);
st{1} = input('enter the first name','s');
st{2} = input('enter the last name','s');
str=[st{:}] %if you want to convert it back to a string
%str = strcat(st{1},'_',st{2}) %if you want a _ between the inputs
end
Try the following code:
st = [];
st{1} = input('enter the first name: ','s');
st{2} = input('enter the last name: ','s');
str = strcat(st{1},'_',st{2})

How to programatically construct a large cell array

How could I construct automatically a dataset like the one below, assuming that the number of columns of matrix summary_whts is approx. 400???
lrwghts = dataset(...
{summary_whts(:,01),'w00'},...
{summary_whts(:,02),'w01'},...
{summary_whts(:,03),'w02'},...
{summary_whts(:,04),'w03'},...
{summary_whts(:,05),'w04'},...
{summary_whts(:,06),'w05'},...
{summary_whts(:,07),'w06'},...
{summary_whts(:,08),'w07'},...
{summary_whts(:,09),'w08'},...
{summary_whts(:,10),'w09'},...
{summary_whts(:,11),'w10'},...
{summary_whts(:,12),'w11'},...
'ObsNames',summary_mthd);
Why not use a simple loop to populate dataset?
nCols = size(summary_whts,1);
dataset = cell(nCols, 2);
for i = 1:nCols
dataset{i,1} = summary_whts(:,i);
dataset{i,2} = sprintf('w%04d', i);
end
dataset{end+1,1} = 'ObsNames';
dataset(end, 2} = summary_mthd;
At last, I found it! This is what I was looking for:
cat = [];
for i = 0:(size(X,2)),
cat = [cat;sprintf('w%03d',i)];
end
cat = cellstr(cat);
lrwghts = dataset({summary_whts,cat{:}},'ObsNames',cellstr(summary_mthd));